How to calculate the number of days between two dates?

  • Thread starter phil_harmonic
  • Start date
In summary: That algorithm is the best I've seen and I understand how to use it to find the Julian Day Number, but I'm still lost on how to find the number of days between any two given dates. Can you help?In summary, the conversation discusses the formula for calculating the number of days between two dates, with one participant suggesting to use Google to find the formula and another pointing out potential errors and exceptions in the process. The conversation also touches on the use of algorithms and programmers in finding the simplest method for computation. Finally, there is a request for the formula in mathematical form to find the number of days between any two given dates.
  • #1
phil_harmonic
8
0
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.
 
Mathematics news on Phys.org
  • #2
Google is your friend. You should think about learning to use it.
 
  • #3
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?
 
  • #4
DivisionByZro said:
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 ... :smile:
 
  • #5
AlephZero said:
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 ... :smile:

That's kind of a small exception for most things. I suppose it all depends on what the OP needs.
 
  • #6
DivisionByZro said:
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.
 
  • #7
phil_harmonic said:
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.
 
  • #8
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
 
  • #9
phinds said:
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?"
 
  • #10
phil_harmonic said:
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
 
  • #11
phyzguy said:
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.
 
  • #12
phil_harmonic said:
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.
 
  • #13
phil_harmonic said:
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.

Code:
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)
 
Last edited:
  • #14
Applejacks01 said:
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.
 
  • #15
phil_harmonic said:
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.
 
  • #16
phil_harmonic said:
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.
 
  • #17
Applejacks01 said:
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).
 
  • #18
phil_harmonic said:
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)
 
Last edited:
  • #19
phil_harmonic said:
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.

Julian days are just numbered sequentially, so if you want the number of days between date A and date B, it is just the difference of the Julian days, i.e. Julian Day[A]-Julian Day. As an example, April 4, 2011 is Julian Day 2455656, and today(Jan 22,2012) is Julian Day 2455949, so the number of days between them is 2455949-2455656 = 293 days.

For what it's worth, here is Mathematica code for calculating Julian Day:

JDN[day_, month_, year_] :=
Module[{a, y, m}, a := IntegerPart[(14 - month)/12];
y := year + 4800 - a; m := month + 12 a - 3;
day + IntegerPart[(153 m + 2)/5] + 365 y + IntegerPart[y/4] -
IntegerPart[y/100] + IntegerPart[y/400] - 32045.0]
 
  • #20
Applejacks01 said:
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) - (365-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) - (365-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

phyzguy said:
Julian days are just numbered sequentially, so if you want the number of days between date A and date B, it is just the difference of the Julian days, i.e. Julian Day[A]-Julian Day. As an example, April 4, 2011 is Julian Day 2455656, and today(Jan 22,2012) is Julian Day 2455949, so the number of days between them is 2455949-2455656 = 293 days.

For what it's worth, here is Mathematica code for calculating Julian Day:

JDN[day_, month_, year_] :=
Module[{a, y, m}, a := IntegerPart[(14 - month)/12];
y := year + 4800 - a; m := month + 12 a - 3;
day + IntegerPart[(153 m + 2)/5] + 365 y + IntegerPart[y/4] -
IntegerPart[y/100] + IntegerPart[y/400] - 32045.0]



Thanks, both of these two answers were sufficient.
 

1. How do I calculate the number of days between two dates?

To calculate the number of days between two dates, you can subtract the earlier date from the later date. This will give you the number of days in between. For example, if you have two dates: January 1, 2021 and January 5, 2021, you would subtract January 1 from January 5, which would give you 4 days in between.

2. Do I need to consider leap years when calculating the number of days between two dates?

Yes, leap years should be taken into consideration when calculating the number of days between two dates. Leap years occur every 4 years, except for years that are divisible by 100 but not 400. To account for this, you can use a formula that takes into account the number of leap years between the two dates.

3. Can I use a calculator to calculate the number of days between two dates?

Yes, you can use a calculator to calculate the number of days between two dates. However, it is important to make sure that the calculator you are using takes into account leap years and other factors that may affect the accuracy of the calculation.

4. Is there an easier way to calculate the number of days between two dates?

There are many online tools and apps that can help you easily calculate the number of days between two dates. These tools take into account leap years and other factors to give you an accurate result. You can also use a spreadsheet program like Microsoft Excel to calculate the number of days between two dates.

5. How do I calculate the number of days between two dates if they are in different years?

If the two dates are in different years, you can still use the same method of subtracting the earlier date from the later date. However, you will need to take into account the number of days in each year between the two dates. For example, if you are calculating the number of days between December 31, 2020 and January 1, 2021, you would need to add 365 days (or 366 if it is a leap year) to the calculation.

Similar threads

Replies
20
Views
1K
Replies
7
Views
1K
  • General Math
Replies
20
Views
2K
  • General Math
Replies
5
Views
959
Replies
3
Views
859
Replies
4
Views
492
  • General Math
2
Replies
66
Views
4K
  • Precalculus Mathematics Homework Help
Replies
3
Views
1K
  • General Math
Replies
5
Views
1K
Replies
1
Views
727
Back
Top