Register to reply 
How to calculate the number of days between two dates? 
Share this thread: 
#1
Jan2112, 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.



#2
Jan2112, 05:57 PM

PF Gold
P: 6,500

Google is your friend. You should think about learning to use it.



#3
Jan2112, 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? 


#4
Jan2112, 06:50 PM

Engineering
Sci Advisor
HW Helper
Thanks
P: 7,284

How to calculate the number of days between two dates?



#5
Jan2112, 07:00 PM

P: 252




#6
Jan2212, 08:59 AM

P: 8




#7
Jan2212, 09:33 AM

PF Gold
P: 6,500

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
Jan2212, 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 


#9
Jan2212, 10:54 AM

P: 8

Why is it that some people say this is a "messy process" whereas others say it is "shockingly easy?" 


#10
Jan2212, 12:25 PM

P: 2,195

http://en.wikipedia.org/wiki/Julian_day 


#11
Jan2212, 01:30 PM

P: 8




#12
Jan2212, 01:31 PM

P: 8




#13
Jan2212, 02:40 PM

P: 26

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, 4Start(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 + (4StartLeapCheck) 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_endEndLeapCheck)  (Y_start+(4StartLeapCheck))/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.
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) + (365Elapsed 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) + (365Elapsed 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_endY_start  1){[((Y_end(Y_end Mod 4))  (Y_start+(4(Y_start Mod 4)))/4] + 1})} + {(Elapsed days within Y_end) + (365Elapsed 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) 


#14
Jan2212, 02:51 PM

P: 8




#15
Jan2212, 02:54 PM

P: 26

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
Jan2212, 03:00 PM

PF Gold
P: 6,500




#17
Jan2212, 03:01 PM

P: 8




#18
Jan2212, 03:19 PM

P: 26

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 adays elapsed in a)} Which simplifies to: 366*(((ba + aMod4 bMod4 4)/4)+1) + 365*((ba1)(((ba + aMod4 bMod4 4)/4)+1) + (days elapsed in b) + (Days in Year adays 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))a4+`mod`(a, 4))+1(1/100)*(b(`mod`(b, 100))a100+`mod`(a, 100))1(1/400)*(b(`mod`(b, 400))a400+`mod`(a, 400))1)+365*(ba1((1/4)*(b(`mod`(b, 4))a4+`mod`(a, 4))+1(1/100)*(b(`mod`(b, 100))a100+`mod`(a, 100))1(1/400)*(b(`mod`(b, 400))a400+`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 