Jump to content
  • Advertisement
Sign in to follow this  
owl

Calculating number of days between two dates.

This topic is 4900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anybody knows a good way to calculate the number of days between two given dates that takes into account leap years? (not using any already existing library, just plain c++) mm/dd/yyyy 12/09/2004 - 02/29/1246 = ?(days) Or better yet, a good way of calculating the correct day of month of a resulting difference between a date and a given number of days. 02/29/1246 - 4936(days) = mm/??/yyyy I'm bored so I'm making a c++ class to handle date operations/parsing as easily as possible, using overloaded operators, etc. I'm not worried about speed/memory performance so I'm planning to store years, months, days, ..., seconds as integers. I was thinking about storing the data as an array of bytes but different processor architectures could be an issue here. I'm planning on releasing it to the public domain if I ever finish it. Any constructive suggestions will be appreciated. [Edited by - owl on January 17, 2005 12:31:37 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by owl
Does anybody knows a good way to calculate the number of days between two given dates that takes into account leap years? (not using any already existing library, just plain c++)

mm/dd/yyyy
12/09/2004 - 02/29/1246 = ?(days)


Couldn't you just do 12/09/2004 - 02/29/1246 + (2004 - 1246) / 4
?

It's late so if the above text makes no sense, sorry :P

Share this post


Link to post
Share on other sites
@load_bitmap_file: Yes that could do the trick, but there is more about leap years than a period of 4 years between each one.

I've been reading some docs, and it seems to be a whole issue with date calculation. Smart people trough centuries have stated the following rules to calculate leap years:

- Most years divisible by 4 are Leap Years (i.e. 1996 was)
- However, most years divisible by 100 are NOT! (1900 was not)
- Unless they are also divisible by 400 (2000 will be)
- Every year which is divisible by 4000 is not a leap year.
source

So I was wondering how am I going to put all this together...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Might help

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://mathforum.org/library/drmath/view/59234.html

Share this post


Link to post
Share on other sites
From Wikipedia:

"The Gregorian calendar adds an extra day to February, making it 29 days long, in years divisible by 4, excluding years divisible by 100, but including years divisible by 400."

That would translate to this in code I believe:

bool IsLeapYear(const int& year)
{
return ((year % 400) == 0) || ((year % 4) == 0) && !((year % 100) == 0);
}

Share this post


Link to post
Share on other sites
Heres a nice way.

count += (int)(year * 365.256 + 0.5)
count += daysinmonth[month]
count += Days

Scount += (int)(Syear * 365.256 + 0.5)
Scount += daysinmonth[smonth]
Scount += Sdays

Daysdif = Scount - Count.

Easy
Fast
Affordible.
Even calculates that extra day every 6000 years!

Buy it now, for just the low low price of $0.00 (+ a +6 rating).

Edit: Silly mistake. Sorry.

From,
Nice coder

[Edited by - Nice Coder on January 17, 2005 3:18:35 AM]

Share this post


Link to post
Share on other sites
We have virtually that exact same code in something at my work, Nice Coder. It works well enough.

Share this post


Link to post
Share on other sites
Its the first thing i thought of. Great minds think alike, eh?


//In init
month[0] = 31
month[1] = 28
month[2] = 30
month[3] = 31
month[4] = 30
month[5] = 31
month[6] = 30
month[7] = 31
month[8] = 30
month[9] = 31
month[10] = 30
month[11] = 31

//in routine

//Days = Paramiter, 16bit int
//Sdays = Same, just for the other date
// m = month number, 0-11
//sm = same, for the other date
Const double dy = 365.256363051 // A sidereal year, which should be what you want. I'm not sure if a double is accurite enough, tho. But it probably won't be a problem, if you arn't going to use it in the year 10Billion.

days += (int)(year * dy + 0.5);
days += month[m];

Sdays += (int)(Syear * dy + 0.5);
Sdays += month[sm];

Return (int16abs(Days - Sdays))


This should be right. (i'm typing it strait into the box, so there might be a few errors).

// Made it more accurite
From,
Nice coder

[Edited by - Nice Coder on January 17, 2005 4:52:32 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Nice Coder
Heres a nice way.

count += (int)(year * 365.256)
count += daysinmonth[month]
count += Days

Scount += (int)(Syear * 365.256)
Scount += daysinmonth[smonth]
Scount += Sdays

Daysdif = Scount - Count.

Easy
Fast
Affordible.
Even calculates that extra day every 6000 years!

Buy it now, for just the low low price of $0.00 (+ a +6 rating).

From,
Nice coder


Thanks everyone for posting in.

I guess "days" and "sdays" are the day number of date and sdate. Your code seems to work amazingly well, but looks like the leap years come 1 year later. I tried the values 2004,31,01 and 2005,31,01 (year, daysinmonth, days) and it returns 365, while 2003,31,01 and 2004,31,01 returns 366. But the leap year was 2004 and not 2003. Should I add 1 to the years passed or am I doing something wrong?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!