473,670 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating number of days between two dates.

I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.

Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
- Clint

Apr 25 '07 #1
9 21380
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.

Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
Sure, you could do that. Have you already tried? If not, why not?
If yes, why do you ask? Did it work?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 25 '07 #2
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.

Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
Well, how many days are between 3pm Friday and 8am Saturday? 0? 1? Some
fraction? You need to decide that first.

Brian
Apr 25 '07 #3
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.

Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
- Clint
The key phrase is "Julian Date," which is a sort of free-running day
counter that counts from a point way far back in prehistory, like 4713
BC. Astronomers use the Julian date, and it's easy to subtract two such
dates to get the number of days. It's up to you how you want to count
partial days.

The only problem with Julian date is that the number is so huge.
Especially back when computers had 16 bits -- or even 8 -- it required
multiple words to store a JD. So several groups and companies chose to
standardize on a date whose basis is not quite so far back. It's my
understanding that Microsoft defined such a date. I guess that's what
they use in their library of functions.

If not, you can always use the true JD. Any good book on astronomy --
or, for that matter, many web sites -- will have pretty short algorithms
for generating the number.

Jack
Jack
Apr 26 '07 #4
On Apr 26, 1:10 am, Jack Crenshaw <jcr...@earthli nk.netwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
- Clint

The key phrase is "Julian Date," which is a sort of free-running day
counter that counts from a point way far back in prehistory, like 4713
BC. Astronomers use the Julian date, and it's easy to subtract two such
dates to get the number of days. It's up to you how you want to count
partial days.

The only problem with Julian date is that the number is so huge.
Especially back when computers had 16 bits -- or even 8 -- it required
multiple words to store a JD. So several groups and companies chose to
standardize on a date whose basis is not quite so far back. It's my
understanding that Microsoft defined such a date. I guess that's what
they use in their library of functions.

If not, you can always use the true JD. Any good book on astronomy --
or, for that matter, many web sites -- will have pretty short algorithms
for generating the number.

Jack
Jack

Jack,
That's pretty interesting. In the end, I didn't do that, but I
appreciate the suggestion.
Thanks.

- Clint

May 22 '07 #5
On Apr 25, 6:37 pm, "Default User" <defaultuse...@ yahoo.comwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?

Well, how many days are between 3pm Friday and 8am Saturday? 0? 1? Some
fraction? You need to decide that first.

Brian
Thanks for bringing up that issue.
I decided to measure between the starts of each day in the date range.

- Clint

May 22 '07 #6
On Apr 25, 5:42 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?

Sure, you could do that. Have you already tried? If not, why not?
If yes, why do you ask? Did it work?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor, Thanks for the response.

I tried it. It seems to work.

I was actually trying to port some C# .NET code we had in one of our
applications to our C++ applications. .NET's datetime class has a
function for determining the number of days between two dates, so I
needed my C++ code to produce the same results. But I was wondering
if I had to handle stuff like daylight savings time, leap year, etc.
myself or whether the difftime() function took all that stuff into
account.

- Clint

May 22 '07 #7
On Apr 25, 6:37 pm, "Default User" <defaultuse...@ yahoo.comwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?

Well, how many days are between 3pm Friday and 8am Saturday? 0? 1? Some
fraction? You need to decide that first.

Brian
Thanks for bringing up that issue.
I decided to measure between the starts of each day in the date range.

- Clint

May 22 '07 #8
On May 22, 8:50 pm, clintonb <cba...@century tel.netwrote:
On Apr 25, 5:42 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
Sure, you could do that. Have you already tried? If not, why not?
If yes, why do you ask? Did it work?
I tried it. It seems to work.
Which, of course, doesn't mean much. Testing to find out
whether something is guaranteed to work is useless; it will only
tell you whether it worked with the exact input values you used,
on the implementation you tested. (This doesn't mean that you
shouldn't test your application, or unit test your individual
components. Only that you shouldn't count exclusively on
testing. And above all, that you shouldn't count at all on
testing to determine whether something is guaranteed by the
standard, on all implementations .)

The problem is that difftime returns the number of seconds
between the two *times*. Nothing about dates. So while the
number of days between May 23, 2007 and May 22, 2007 is 1,
that's not what you'll get if you simply do a difftime between
1:00 May 23, 2007 and 23:00 May 22, 2007. And any rounding
algorithm which will return 1 then will probably return 2 when
you compare 23:00 May 23, 2007 and 1:00 May 22, 2007. In order
to get correct results, you probably have to force the time_t to
a common time, by breaking them out into a tm struct, forcing
the tm_hour, tm_min and tm_sec to a "standard" value, then using
mktime to get a new time_t, and doing difftime on those. (Note
too that some systems store time_t in UTC, so local time zones,
summer time, etc., may have an influence on the results.)
I was actually trying to port some C# .NET code we had in one of our
applications to our C++ applications. .NET's datetime class has a
function for determining the number of days between two dates, so I
needed my C++ code to produce the same results. But I was wondering
if I had to handle stuff like daylight savings time, leap year, etc.
myself or whether the difftime() function took all that stuff into
account.
difftime gives the number of seconds between two times. There
are no leap years, daylight savings time, etc. in the number of
seconds. There are leap seconds, however (although some systems
choose to ignore them).

If you're only concerned about local time, calling localtime on
the two values, forcing the time to noon, then using mktime to
convert back to time_t, difftime on those, and dividing by the
number of seconds in a day, and rounding the results to nearest
should be adequate. The results of difftime could be off about
an hour, if there was a shift between summer time and standard
time in the interval; the error should never be enough that
rounding to an integral number of days doesn't give the correct
results, however.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 23 '07 #9
On May 22, 6:37 pm, clintonb <cba...@century tel.netwrote:
On Apr 25, 6:37 pm, "Default User" <defaultuse...@ yahoo.comwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
Well, how many days are between 3pm Friday and 8am Saturday? 0? 1? Some
fraction? You need to decide that first.
Thanks for bringing up that issue.
I decided to measure between the starts of each day in the date range.
Intuitively, I'd go for noon. An off by one hour error, due
maybe because of a switch between summer time and standard time,
won't cause a change of day. (In practice, we use the start of
the day here, in code that was written many, many years ago, and
it has never caused problems. On the other hand, we use UTC, so
there is no summer time, and we are locked into Posix, so we
also know that time_t represents seconds directly, and we do the
arithmetic directly on it.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 23 '07 #10

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

Similar topics

4
4469
by: Hans Gruber | last post by:
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...
4
12674
by: John | last post by:
hey all..... alright, I am frusterated to the point of throwing my machine out the window (this board went down, trying to find stuff on google, this has been a nightmare) so I hope you guys can help me with a solution. This is what I am trying to do: Pretty much, there are two dates in my database, a start date, and an end
10
6633
by: riki | last post by:
hello, i need to calculate num of days between 2 dates... i get separate parts of dates from html form, then i need to "make" begining and ending date and calculate difference between them... something like this, in sybase: DAYS(YMD(?YEAR?,?MONTH?,?DAY?), YMD(?YEAR1?,?MONTH1?,?DAY1?)) thnx
2
3151
by: celsius | last post by:
Hi folks, Al Bowers wrote this program on comp.lang.c Date: 2001-07-09 13:41:58 PST #include <stdio.h> int isleap (unsigned yr); static unsigned months_to_days (unsigned month); static long years_to_days (unsigned yr); long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day);
2
2681
by: Scott | last post by:
Hi All I have an application that has a start date and a finish date I need to calculate the number of days from the start date to the end date and then display this in a read only text box... How might I code this?... Regards Scott
4
1476
by: Yotam | last post by:
Hi, I need some help with JS. I will be grateful, if you can help me out. I have two date fields (check in, check out) and "number of days" field. I want the script to calculate automatically the difference. For example: I have defaults dates, and I want the script to put the difference in "number of days" field. And if the user will change the date, the number of days will change automatically.
1
12934
by: island82 | last post by:
I have two dates: 1. mfg date 2. incident date I need to calculate the number of days 2 and 1. This is what i have: <xsl:when test="Complaint/V_CXP_CUSTOMER_PXP/CXP_MFG_DATE ='CXP_MFG_DATE'"> <C20_Age_of_Device><xsl:value-of select="V_CXP_CUSTOMER_PXP/CXP_AWARE_DATE='CXP_AWARE_DATE - CXP_MFG_DATE'"></C20_Age_of_Device> that is to say, if the mfg date is available, then calculate the number of days between...
8
7502
by: =?Utf-8?B?QWw=?= | last post by:
I am working in vb2005. how can I calculate business days (not including holidays and weekends) between 2 dates? thanks Al
5
3164
by: jjkeeper | last post by:
G'day, I'm currently working on an annual leave database for the company, so far so good, till they want the database to be able to identify public holidays and prevent reducing the employee's balance if the application overlaps with the holiday. I'm using access 2000. Here's what I have so far. tblMainProfile - contains employee's profile EmployeeID <== Pkey Name Date of Birth .. tblLeaveApply - link to tblMainProfile by...
0
8469
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
8903
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
8661
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
6213
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
5684
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
4211
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
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2800
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
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.