473,594 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

date/time calculations: calculating exact difference

Hi all,

I have been struggling with a problem all day, I have been unable to come
up with a working solution.

I want to write a function which takes 2 unix timestamps and calculates
the difference. I want it to return the difference in years, months, days,
hours, minutes and seconds (a complete summary). Keeping into account of
course that these are 2 real dates, I dont want it to work with 30.475 as
an average number of days in a month, I want it to work with the supplied
months/years.

The output ought to be: there is 0 years, 7 months, 12 days, 5 hours,
56 minutes and 51 seconds difference.
(I dont want the difference as in 1237438424 seconds but a complete
summary).

Any of my attempts have failed. It is easy to calculate the numbers of
seconds, minutes, hours and days (total only). But I cant get passed
calculating the number of months and years. Furthermore, any of the
libraries from the internet which I have tested seem to be flawed as well,
their results are not precise.

Can this not be done? Anyone?

Kind regards,

Hans Gruber
Jul 17 '05 #1
4 4450
Hans Gruber wrote:
Hi all,

I have been struggling with a problem all day, I have been unable to come
up with a working solution.

I want to write a function which takes 2 unix timestamps and calculates
the difference. I want it to return the difference in years, months, days,
hours, minutes and seconds (a complete summary). Keeping into account of
course that these are 2 real dates, I dont want it to work with 30.475 as
an average number of days in a month, I want it to work with the supplied
months/years.

The output ought to be: there is 0 years, 7 months, 12 days, 5 hours,
56 minutes and 51 seconds difference.
(I dont want the difference as in 1237438424 seconds but a complete
summary).


Look into the date functions. There are functions in there to tell you
how many days are in the current month. Even if you wanted to do this
by hand you could create an array with each month and how many days it
has and check for a leap year.

Then use the date function to work with these.... It will work, but to
get it in your exact format might be a bit of work.

Mike
Jul 17 '05 #2
On Sun, 15 May 2005 20:58:54 -0500, Mike Willbanks wrote:
Hans Gruber wrote:
Hi all,

I have been struggling with a problem all day, I have been unable to come
up with a working solution.

I want to write a function which takes 2 unix timestamps and calculates
the difference. I want it to return the difference in years, months, days,
hours, minutes and seconds (a complete summary). Keeping into account of
course that these are 2 real dates, I dont want it to work with 30.475 as
an average number of days in a month, I want it to work with the supplied
months/years.

The output ought to be: there is 0 years, 7 months, 12 days, 5 hours,
56 minutes and 51 seconds difference.
(I dont want the difference as in 1237438424 seconds but a complete
summary).


Look into the date functions. There are functions in there to tell you
how many days are in the current month. Even if you wanted to do this
by hand you could create an array with each month and how many days it
has and check for a leap year.

Then use the date function to work with these.... It will work, but to
get it in your exact format might be a bit of work.

Mike


Thanks Mike, I was actually thinking up something like that right before I
went to bed last night. Another day of hard work coming up ;)

Hans
Jul 17 '05 #3
I noticed that Message-ID: <pa************ *************** *@nakatomi.de>
from Hans Gruber contained the following:
Thanks Mike, I was actually thinking up something like that right before I
went to bed last night. Another day of hard work coming up ;)


It's easy as you say, to convert seconds into minutes hours days and
weeks. But if you said months what would you mean? Often the secret to
coding something is the correct identification of the problem.

Let's say I want to work out how many months it is till my next birthday
(March 5th). What I'd probably do is ignore the month length difference
and count up. It's May 16th today and so it's 9 months till February
16th. Then it's 13 days till my birthday.

If that's the way you want to work it out then you have your logic. I'm
not going to code it but I think I'd be looking carefully at mktime() if
I was.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #4
On Mon, 16 May 2005 07:54:48 +0100, Geoff Berrow
<bl******@ckdog .co.uk> wrote:
I noticed that Message-ID: <pa************ *************** *@nakatomi.de>
from Hans Gruber contained the following:
Thanks Mike, I was actually thinking up something like that right before I
went to bed last night. Another day of hard work coming up ;)
It's easy as you say, to convert seconds into minutes hours days and
weeks. But if you said months what would you mean? Often the secret to
coding something is the correct identification of the problem.

Let's say I want to work out how many months it is till my next birthday
(March 5th). What I'd probably do is ignore the month length difference
and count up. It's May 16th today and so it's 9 months till February
16th. Then it's 13 days till my birthday.


If your birthday was 30 June and today was 31 May would that be 1
month or just 30 days until your birthday?

Although if you decided the answer was "1 month", be aware you'd get
the same answer on 30 May.

There isn't really a "correct" answer, but the OP should consider
these edge cases.

However, it depends what data represents and how the user will use it.
If long differences are shown to a lower resolution, this anomaly may
not matter. If the time difference is several months, the user may not
be interested in the value down to the minute and second.

For example:

DIFFERENCE :- WHAT TO SHOW 60 days :- months, days.

< 60 days :- days only.
< 7 days :- days, hours.
< 1 day :- hours, minutes.
< 6 hours :- hours, minutes, seconds.

--
David ( @priz.co.uk )
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2025
by: Marc | last post by:
I've got a PHP script that's using fopen to connect to a remote web server and pick up data. I've run into a problem in calculating elapsed time. The remote web server outputs a time and I'd like to calculate the difference between that time and the current time. The problem is that the time on the remote server isn't in sync with the time on my server (off by about 5 minutes) and this throws off the elapsed time calculations. I know...
17
2501
by: clintonG | last post by:
When the following code is run on Sat Dec 25 2004 19:54:18 GMT-0600 (Central Standard Time) var today = new Date(); var GMTDate = today.toGMTString(); document.write("GMTDate: " + GMTDate); The code returns: Sun, 26 Dec 2004 19:54:18 GMT (the next day after the code was actually run)
11
4635
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m having a heck of a time doing so. I have created a form where I use drop downs do specify year, month, date, hour, minute and seconds. When the form is loaded, the dropdowns have to display the proper values for the current time zone type. This
0
2004
by: Yuri Ludevig | last post by:
I recently was asked to switch from using raw lv's to file system and the first problem I encountered was with calculating the exact size for tablespaces. This is an example: I create LV and mount a file system over it. Let's say my page size is 16K. I tried to divide the free space (from df -k command) and use this number in my create tablespace command. As it turned out I couldn't use all the space (in case of 4.6 G file there was...
2
6507
by: Julie Wardlow | last post by:
Help! I am calculating a future date using the DateAdd function in a query (the calculation also involves an IIf statement), and have managed to get this formula to produce the required result. I then want to search through the records and select those with dates (as caluclated above) within a user defined range, and so I am using a parameter query. However, this query returns dates outside of the range and appears to have particular...
8
2107
by: Charlie Brookhart | last post by:
I am creating a program that involves having to find the difference between two dates and converting it to a number to be used for calculations. The problem is that the way it is setup, VB is not doing anything with the dates that I have selected. I hope that someone here can help me figure out why it is not working what I have to do to make it work. This the code I have for the date calculations. txtNumberOfDays.text =...
4
15711
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual way, i.e....
1
1851
by: b.beeching | last post by:
Not sure if my subject is entirely accurate but here goes. I need to calculate the date difference between a date A and a date B... however date B relies entily on date A. EG: i have an advert in a paper (The Daily Telegraph say), now i need to know the next and previos dates that this occured. I can do this very easily for a one time calculations, however i need to do this for many 000's of rows.
44
10165
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
0
7941
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8246
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8368
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8231
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5404
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3895
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.