Register to reply

How to calculate the number of days between two dates?

by phil_harmonic
Tags: dates, days, number
Share this thread:
phil_harmonic
#1
Jan21-12, 04:59 PM
P: 8
What is the formula for this? I know there must be one even if it requires weird constants that are defined by piecewise functions of the truth values of the presence of certain months.
Phys.Org News Partner Mathematics news on Phys.org
Math journal puts Rauzy fractcal image on the cover
Heat distributions help researchers to understand curved space
Professor quantifies how 'one thing leads to another'
phinds
#2
Jan21-12, 05:57 PM
PF Gold
phinds's Avatar
P: 6,500
Google is your friend. You should think about learning to use it.
DivisionByZro
#3
Jan21-12, 06:36 PM
P: 252
This almost sounds like a programming question. It's shockingly easy to calculate the number of days between two dates...Turn your months and year into days, and then consider most months separately (i.e. 4th of Feb 2012 = 31 + 4 + 0 +... = 35th day.)

Could you not have googled this?

AlephZero
#4
Jan21-12, 06:50 PM
Engineering
Sci Advisor
HW Helper
Thanks
P: 7,284
How to calculate the number of days between two dates?

Quote Quote by DivisionByZro View Post
This almost sounds like a programming question. It's shockingly easy to calculate the number of days between two dates...
It's "shockingly easy" to do it wrong, but you only test it over the last few years you probably wouldn't notice things like the year after 1BC is 1AD, not "year 0". Or the different numbers of days that went missing in different countries when the Gregorian calendar was introduced (and that was less than 100 years ago, in one European country.) Or that during the changeover period, the date of 30 February was valid in some countries for one particular year. Or .......
DivisionByZro
#5
Jan21-12, 07:00 PM
P: 252
Quote Quote by AlephZero View Post
It's "shockingly easy" to do it wrong, but you only test it over the last few years you probably wouldn't notice things like the year after 1BC is 1AD, not "year 0". Or the different numbers of days that went missing in different countries when the Gregorian calendar was introduced (and that was less than 100 years ago, in one European country.) Or that during the changeover period, the date of 30 February was valid in some countries for one particular year. Or .......
That's kind of a small exception for most things. I suppose it all depends on what the OP needs.
phil_harmonic
#6
Jan22-12, 08:59 AM
P: 8
Quote Quote by DivisionByZro View Post
This almost sounds like a programming question. It's shockingly easy to calculate the number of days between two dates...Turn your months and year into days, and then consider most months separately (i.e. 4th of Feb 2012 = 31 + 4 + 0 +... = 35th day.)

Could you not have googled this?
I know how to go about figuring it out and I realize this is a basic question, but I am looking for a formula to do it, specifically one that you can use for manual computation. I tried googling, but everything that came up had to do with programming or excel; there were no mathematical formulas that could be used to compute by hand. I know how to do it the way you described, I'm just wondering if there is not a faster way.
phinds
#7
Jan22-12, 09:33 AM
PF Gold
phinds's Avatar
P: 6,500
Quote Quote by phil_harmonic View Post
I know how to go about figuring it out and I realize this is a basic question, but I am looking for a formula to do it, specifically one that you can use for manual computation. I tried googling, but everything that came up had to do with programming or excel; there were no mathematical formulas that could be used to compute by hand. I know how to do it the way you described, I'm just wondering if there is not a faster way.
Uh ... dude ... where do you think programmers get their algorithms? There ARE no algorithims other than what programmers use and if there WERE, then programmers would use them.

If you can't do something without a computer then a computer will just let you get the wrong answer a lot faster.

Getting what you want is unfortunately a messy process. Computer programmers always use the simplest possible method (if they are any good), so theirs is the best you'll find even though they look pretty awful. All those leap years and things.
phyzguy
#8
Jan22-12, 10:29 AM
P: 2,195
The algorithm for calculating Julian Day from the Gregorian calendar date given in Wikipedia (below) is quite simple. One then gets the number of days between the two dates by simple subtraction. It doesn't get any simpler.

http://en.wikipedia.org/wiki/Julian_day
phil_harmonic
#9
Jan22-12, 10:54 AM
P: 8
Quote Quote by phinds View Post
Uh ... dude ... where do you think programmers get their algorithms? There ARE no algorithims other than what programmers use and if there WERE, then programmers would use them.

If you can't do something without a computer then a computer will just let you get the wrong answer a lot faster.

Getting what you want is unfortunately a messy process. Computer programmers always use the simplest possible method (if they are any good), so theirs is the best you'll find even though they look pretty awful. All those leap years and things.
Fine, but the algorithms google gives me just require the computer to lookup a certain day without telling how it finds the Julian Day Number. I'd be fine with the algorithm programmers use but I can't seem to find this in mathematical form anywhere. If you could either direct me to a link or typeset the formula here, I'd appreciate it. Also, I'm not concerned with years so much as months and days. An example problem I would use the formula for would be as follows: find the number of days between now and April 4,2011.

Why is it that some people say this is a "messy process" whereas others say it is "shockingly easy?"
phyzguy
#10
Jan22-12, 12:25 PM
P: 2,195
Quote Quote by phil_harmonic View Post
Fine, but the algorithms google gives me just require the computer to lookup a certain day without telling how it finds the Julian Day Number. I'd be fine with the algorithm programmers use but I can't seem to find this in mathematical form anywhere. If you could either direct me to a link or typeset the formula here, I'd appreciate it.
Again, the algorithm for finding the Julian day from the date is given here:

http://en.wikipedia.org/wiki/Julian_day
phil_harmonic
#11
Jan22-12, 01:30 PM
P: 8
Quote Quote by phyzguy View Post
Again, the algorithm for finding the Julian day from the date is given here:

http://en.wikipedia.org/wiki/Julian_day
Again, how would this help me find the number of days between today and April 4, 2011 or any other day within the last decade. Thanks.
phil_harmonic
#12
Jan22-12, 01:31 PM
P: 8
Quote Quote by phil_harmonic View Post
Computer programmers always use the simplest possible method (if they are any good), so theirs is the best you'll find even though they look pretty awful. All those leap years and things.
I understand, I just want the formula in a math format, not excel or php or any of that because that doesn't do me any good.
Applejacks01
#13
Jan22-12, 02:40 PM
P: 26
Quote Quote by phil_harmonic View Post
Fine, but the algorithms google gives me just require the computer to lookup a certain day without telling how it finds the Julian Day Number. I'd be fine with the algorithm programmers use but I can't seem to find this in mathematical form anywhere. If you could either direct me to a link or typeset the formula here, I'd appreciate it. Also, I'm not concerned with years so much as months and days. An example problem I would use the formula for would be as follows: find the number of days between now and April 4,2011.

Why is it that some people say this is a "messy process" whereas others say it is "shockingly easy?"
Here is my work and explanation. Please let me know if any errors are spotted, thanks.

Algorithm for determining the number of days between 2 dates

We will use the Mod function, where i Mod j gives us the remainder of (i/j)

Assuming (Year Mod 4) = 0 means the year is a leap year, then we have the following algorithm for our boundary dates:
Let Y_start be our starting year, February_Start be the # of days in Y_start's February, Y_end be our ending year, and February_End be the # of days in Y_end's February

If Y_Start Mod 4 = 0, then February_Start has 29 days. Else, 28 days
Store (Y_Start Mod 4) as StartLeapCheck
If Y_end Mod 4 = 0, then February_End has 29 days. Else, 28 days
Store (Y_end Mod 4) as EndLeapCheck

What we have done is checked to see if our interval is BOUNDED by leap years, and if it is, then we have made the proper adjustments to February in our boundary years. However, now we need to determine how many years WITHIN our interval are year leaps(not including the boundary years).

So, how do we interpret StartLeapCheck and EndLeapCheck ?


They tell us how many years PAST the most recent Leap years(including the boundary dates) the dates are. Example: 2000 Mod 4 = 0, which means we are 0 years past the most recent leap year. 2002 Mod 4 = 2, means we are 2 years past the last leap year.
We also know how many years AWAY from future leap years we are by the compliment, 4-Start(End)LeapCheck).

Let us place a constraint on EndLeapCheck though. If EndLeapCheck = 0, then we shall make EndLeapCheck = 4. I will explain below.
From Y_start, the next future leap year is Y_start + (4-StartLeapCheck)
From Y_end, the next previous leap year is Y_end - (EndLeapCheck)
To determine how many leap years are WITHIN our interval, we will use the following formula:
[((Y_end-EndLeapCheck) - (Y_start+(4-StartLeapCheck))/4] + 1

The +1 is because the first term under states the # of years by 1.
This algorithm will work in all cases, such as when one, both, or neither of the boundaries are leap years, and no matter the distance between Y_end and Y_start.

Example: If we have Y_start = 2002 and Y_end = 2003, then we have 
[((2003-3) - (2002+(4-2))/4] + 1
= -1 + 1 = 0
Also, if we have Y_start = 2002 and Y_end = 2002, then we have:
 [((2002-2) - (2002+(4-2))/4] + 1
= -1 + 1 = 0
and if we have Y_start = 2000 and Y_end = 2003 we have:
 [((2003-3) - (2000+(4))/4] + 1
=-1 +1 = 0
And if we have Y_start = 2000 and Y_end = 2004 we have:
 [((2004-4(remember, we made EndLeapCheck =4) ) - (2000+(4-0))/4] + 1
= (2000-2004)/4 +1
= -1 + 1 = 0
Also note the algorithm will work when the distance between the dates is greater than 4:
Y_start = 2002 and Y_end = 2031
[((2031-3) - (2002+(2))/4] + 1
= (2028-2004)/4 + 1
= (24/4) + 1
= 7
Okay, enough with the examples. Now, how do we apply these results?

Simple:
Figure out the distance between Y_end and Y_start:
Distance =( Y_end - Y_start) -1.
We subtract one so that we do not include either of the endpoints.

So, the number of days between the 2 years is:
(# of Leap Years)*366 + (Distance - # of Leap Years)*365

The last thing we have to do is simple:
(Elapsed days within Y_end) + (365-Elapsed Days within Y_start)

That is calculated by Summing up the months for each year starting from January, and when we hit the desired month, we use the specified date and stop there.
So generally speaking, the formula is: (# of Leap Years)*366 + (Distance - # of Leap Years)*365
+ (Elapsed days within Y_end) + (365-Elapsed Days within Y_start)


And the actual algorithm is:
366*{[((Y_end-(Y_end Mod 4)) - (Y_start+(4-(Y_start Mod 4)))/4] + 1} + 365*({Y_end-Y_start - 1)-{[((Y_end-(Y_end Mod 4)) - (Y_start+(4-(Y_start Mod 4)))/4] + 1})} + {(Elapsed days within Y_end) + (365-Elapsed Days within Y_start)} , where {Y_end Mod 4) = 4 if Y_end Mod 4 is 0, else Y_end Mod 4, February_end = 29 if Y_end Mod 4 = 0(28 otherwise), February_start = 29 if Y_start Mod 4 = 0(28 otherwise)
phil_harmonic
#14
Jan22-12, 02:51 PM
P: 8
Quote Quote by Applejacks01 View Post
Here is my work and explanation. Please let me know if any errors are spotted, thanks.

Algorithm for determining the number of days between 2 dates
We will use the Mod function, where i Mod j gives us the remainder of (i/j)
Assuming (Year Mod 4) = 0 means the year is a leap year, then we have the following algorithm for our boundary dates:
Let Y_start be our starting year, February_Start be the # of days in Y_start's February, Y_end be our ending year, and February_End be the # of days in Y_end's February
If Y_Start Mod 4 = 0, then February_Start has 29 days. Else, 28 days
Store (Y_Start Mod 4) as StartLeapCheck
If Y_end Mod 4 = 0, then February_End has 29 days. Else, 28 days
Store (Y_end Mod 4) as EndLeapCheck
What we have done is checked to see if our interval is BOUNDED by leap years, and if it is, then we have made the proper adjustments to February in our boundary years. However, now we need to determine how many years WITHIN our interval are year leaps(not including the boundary years).
So, how do we interpret StartLeapCheck and EndLeapCheck ?
They tell us how many years PAST the most recent Leap years(including the boundary dates) the dates are. Example: 2000 Mod 4 = 0, which means we are 0 years past the most recent leap year. 2002 Mod 4 = 2, means we are 2 years past the last leap year.
We also know how many years AWAY from future leap years we are by the compliment, 4-Start(End)LeapCheck).
Let us place a constraint on EndLeapCheck though. If EndLeapCheck = 0, then we shall make EndLeapCheck = 4. I will explain below.
From Y_start, the next future leap year is Y_start + (4-StartLeapCheck)
From Y_end, the next previous leap year is Y_end - (EndLeapCheck)
To determine how many leap years are WITHIN our interval, we will use the following formula:
[((Y_end-EndLeapCheck) - (Y_start+(4-StartLeapCheck))/4] + 1
The +1 is because the first term under states the # of years by 1.
This algorithm will work in all cases, such as when one, both, or neither of the boundaries are leap years, and no matter the distance between Y_end and Y_start.
Example: If we have Y_start = 2002 and Y_end = 2003, then we have
[((2003-3) - (2002+(4-2))/4] + 1
= -1 + 1 = 0
Also, if we have Y_start = 2002 and Y_end = 2002, then we have:
[((2002-2) - (2002+(4-2))/4] + 1
= -1 + 1 = 0
and if we have Y_start = 2000 and Y_end = 2003 we have:
[((2003-3) - (2000+(4))/4] + 1
=-1 +1 = 0
And if we have Y_start = 2000 and Y_end = 2004 we have:
[((2004-4(remember, we made EndLeapCheck =4) ) - (2000+(4-0))/4] + 1
= (2000-2004)/4 +1
= -1 + 1 = 0
Also note the algorithm will work when the distance between the dates is greater than 4:
Y_start = 2002 and Y_end = 2031
[((2031-3) - (2002+(2))/4] + 1
= (2028-2004)/4 + 1
= (24/4) + 1
= 7
Okay, enough with the examples. Now, how do we apply these results?
Simple:
Figure out the distance between Y_end and Y_start:
Distance =( Y_end - Y_start) -1
We subtract one so that we do not include either of the endpoints.
So, the number of days between the 2 years is:
(# of Leap Years)*366 + (Distance - # of Leap Years)*365
The last thing we have to do is simple:
(Elapsed days within Y_end) - (Elapsed Days within Y_start)
That is calculated by Summing up the months for each year starting from January, and when we hit the desired month, we use the specified date and stop there.
So generally speaking, the formula is: (# of Leap Years)*366 + (Distance - # of Leap Years)*365
+ (Elapsed days within Y_end) - (Elapsed Days within Y_start)

And the actual algorithm is:
366*{[((Y_end-(Y_end Mod 4)) - (Y_start+(4-(Y_start Mod 4)))/4] + 1} + 365*({Y_end-Y_start)*( *{[((Y_end-(Y_end Mod 4)) - (Y_start+(4-(Y_start Mod 4)))/4] + 1})} + {(Elapsed days within Y_end) - (Elapsed Days within Y_start)} , where {Y_end Mod 4) = 4 if Y_end Mod 4 is 0, else Y_end Mod 4, February_end = 29 if Y_end Mod 4 = 0(28 otherwise), February_start = 29 if Y_start Mod 4 = 0(28 otherwise)
This is great, thank you for providing a satisfactory answer to my question. My only question is: is there a way to write this mathematically instead of using code or pseudocode? Thank you for your help.
Applejacks01
#15
Jan22-12, 02:54 PM
P: 26
Quote Quote by phil_harmonic View Post
This is great, thank you for providing a satisfactory answer to my question. My only question is: is there a way to write this mathematically instead of using code or pseudocode? Thank you for your help.
No problem, I liked the challenge.

I'll see if there is any way to make it more mathy. Keep in mind however that even mathematicians express math in very strange ways, such as the absolute value function, sign function, dirac delta, heaviside, piecewise, etc. This is similar to a "weird" function.
phinds
#16
Jan22-12, 03:00 PM
PF Gold
phinds's Avatar
P: 6,500
Quote Quote by phil_harmonic View Post

Why is it that some people say this is a "messy process" whereas others say it is "shockingly easy?"
I think that's 'cause the ones who are saying it is easy don't understand your question. They think you just want A Julian day, not a difference.
phil_harmonic
#17
Jan22-12, 03:01 PM
P: 8
Quote Quote by Applejacks01 View Post
No problem, I liked the challenge.

I'll see if there is any way to make it more mathy. Keep in mind however that even mathematicians express math in very strange ways, such as the absolute value function, sign function, dirac delta, heaviside, piecewise, etc. This is similar to a "weird" function.
Great, I'm fine with all of those functions (they were what I was referring to when I said a "weird" function).
Applejacks01
#18
Jan22-12, 03:19 PM
P: 26
Quote Quote by phil_harmonic View Post
Great, I'm fine with all of those functions (they were what I was referring to when I said a "weird" function).
Okay, how's this:
We'll clean this up a bit by using interval notation.
Start Date = a
End Date = b
So we are working on the interval [a,b]

366*{[[(b-(b Mod 4)) - (a+(4-(a Mod 4)))]/4] + 1} + 365*({b -a -1)-{[[(b-(b Mod 4)) - (a+(4-(a Mod 4)))]/4] + 1} + {(days elapsed in b) + ((Days in Year a-days elapsed in a)}

Which simplifies to: 366*(((b-a + aMod4 -bMod4 -4)/4)+1) + 365*((b-a-1)-(((b-a + aMod4 -bMod4 -4)/4)+1) + (days elapsed in b) + (Days in Year a-days elapsed in a)

where b Mod 4 = 4 if b Mod 4 is 0, else = b Mod 4, February_b = 29 if b Mod 4 = 0(28 otherwise), February_a = 29 if Y_a Mod 4 = 0(28 otherwise)

You can manipulate that however you wish, but I think that is pretty condensed and useable

Edit: When dealing with dates that contain years divisible by 100 but not divisible by 400, the following formula works. The same constraints on B hold. i.e, when any of the b mod i's = 0 (where i = 4, 100, or 400), b mod i = i.

366*((1/4)*(b-(`mod`(b, 4))-a-4+`mod`(a, 4))+1-(1/100)*(b-(`mod`(b, 100))-a-100+`mod`(a, 100))-1-(1/400)*(b-(`mod`(b, 400))-a-400+`mod`(a, 400))-1)+365*(b-a-1-((1/4)*(b-(`mod`(b, 4))-a-4+`mod`(a, 4))+1-(1/100)*(b-(`mod`(b, 100))-a-100+`mod`(a, 100))-1-(1/400)*(b-(`mod`(b, 400))-a-400+`mod`(a, 400))-1))+(days elapsed in b)+(days in a minus elapsed days in a)


Register to reply

Related Discussions
Calculate Number of Pulses ? Engineering, Comp Sci, & Technology Homework 3
Calculate number of molecules Introductory Physics Homework 3
Looking for a method to calculate what number are used to make a number General Math 5
Why the number of days in a month? General Discussion 23
C++ output date in number of days per year Computing & Technology 6