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.
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?
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 .......
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.
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
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?"
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.
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.
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 (Text): 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)
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.
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.
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)
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]