Calculating number of days between two dates.

Started by
39 comments, last by owl 19 years, 3 months ago
Quote:Original post by Nice Coder
Its the first thing i thought of. Great minds think alike, eh?

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


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


Oups.... I believe this should be
month[0] = 31;month[1] = 28;month[2] = 31; // march is 31 daysmonth[3] = 30;month[4] = 31;month[5] = 30;month[6] = 31; // july is 31 daysmonth[7] = 31; // august is 31 days toomonth[8] = 30;month[9] = 31;month[10] = 30;month[11] = 31;


Regards,
Advertisement
You should use cumulative months and you should use different month-array for leap years [1]. You should base the year length on the rules of computing leap years instead of sidereal year [2]. You should do pure integer math instead of float math to make sure you won't get errors due to lack of accuracy [3]. Ugh.

[1] Think about it, if just for a second. The way you're doing it now, the difference between January 1st and December 1st of same year will be 0 days.

[2] I'll go easy on you: 365 + 1/4 - 1/100 + 1/400 - 1/4000

[3] Just multiply the above summation by "year" without using floats. I know you can do it!
Quote:Original post by Nemesis2k2
That would translate to this in code I believe:

*** Source Snippet Removed ***
This kind of exception chain screams for XOR.
Quote:Original post by civguy
You should use cumulative months


Yes I became aware of this right after I replied Zahlman. :)

Quote:Original post by civguy
and you should use different month-array for leap years


I didn't think of this tough... tnx :) EDIT: Maybe incrementing the first array values by one (after january) on leap years could work too?

Quote:Original post by civguy
[1]. You should base the year length on the rules of computing leap years instead of sidereal year [2]. You should do pure integer math instead of float math to make sure you won't get errors due to lack of accuracy [3]. Ugh.

[1] Think about it, if just for a second. The way you're doing it now, the difference between January 1st and December 1st of same year will be 0 days.

[2] I'll go easy on you: 365 + 1/4 - 1/100 + 1/400 - 1/4000

[3] Just multiply the above summation by "year" without using floats. I know you can do it!


Yes, this is what I was planning to do, but Nice Coder's code looked quite nice, at least until 1500+- BC. Anyway your advice feels sane, I think I'll follow it. :)

How about date dec/incrementation by a given amount of days? Should I calculate the amount of years a certain number of days represent, add that number to the original date's year value and then increment months and days with the reminding days? (always, somehow, taking leap days into account?)

Thanks for your help. You're being rated.
[size="2"]I like the Walrus best.
Quote:Original post by civguy
Quote:Original post by Nemesis2k2
That would translate to this in code I believe:

*** Source Snippet Removed ***
This kind of exception chain screams for XOR.

Nice catch. It doesn't preserve every condition, but you get away with it because something that's divisible by 100 is always divisible by 4, so that case never occurs.
If you do what UNIX does, and store all your dates internally as the number of seconds elapsed since some epochal date, then calculating intervals becomes a piece of cake, as leap years don't even come into the equation.

The only time you'd have to worry about leap years is when you're calling ToString() or something like that.
daerid@gmail.com
Quote:Original post by owl
How about date dec/incrementation by a given amount of days? Should I calculate the amount of years a certain number of days represent, add that number to the original date's year value and then increment months and days with the reminding days? (always, somehow, taking leap days into account?)
An easy and not too slow way to do it is to jump to a date that is "close" to the desired date (using floating point math and ignoring leap years), then calculate the day difference between this approximate date and the original date. See how much it differs from the amount of days you were supposed to jump. Then adjust the approximate date accordingly.
Quote:Original post by daerid
The only time you'd have to worry about leap years is when you're calling ToString() or something like that.
An accurate conversion from seconds to year,month and day is much harder though, than the conversion in another direction which has been discussed here.
Quote:Original post by civguy
Quote:Original post by daerid
The only time you'd have to worry about leap years is when you're calling ToString() or something like that.
An accurate conversion from seconds to year,month and day is much harder though, than the conversion in another direction which has been discussed here.


Not that I necessarily disagree with you, but could you explain your reasons for that?
daerid@gmail.com
Quote:Original post by Zahlman
Shouldn't the 'daysinmonth' be cumulative? o_O
Actually Zahlman is 100% correct here. I should know, I have a perfect working solution using this exact technique at work and the months are cumulative.
i.e.
const int daysUntilMonth[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};dayCount1 = (int)(year1 * 365.256) + daysUntilMonth[month1-1] + Days1;dayCount2 = (int)(year2 * 365.256) + daysUntilMonth[month2-1] + Days2;diff = dayCount2 - dayCount1;
This is used because it is for an old embedded C compiler, not C++, no boost or anything. It does work, and it works well enough considering our product will fall over in the year 2133 anyway.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement