Deltadays and date functions.

05132017, 08:57 PM
Post: #27




RE: Deltadays and date functions.
(05132017 07:15 PM)Vtile Wrote: ....much... I have read this a few times but I am still not sure if I understand you correctly. So here is a short description of the way the JDN formula works. Forget about everything else and give it a try: First of all we define that a year starts with 1 March. This way the irregularity in February appears at the very end of a year where it does not matter if for the last day of the year 28 or 29 days are finally added. So January and February are considered months 13 and 14 of the previous year (m:=m+12, y:=y1). Since 1 March of "year 0" there were y full years with 365 days each, i.e. 365*y days. Plus one additional day every 4 years, i.e. int(y/4) more days. Remember that Jan and Feb belong to the previous year, so if y is a leap year the one more day is only added for dates in March and later: if y is divisible by 4, y–1 is not and one less day is added. This way we get the number of days between 1 March 0 and 1 March of year y. Next we add the number of days from 1 March in year y to the first day of month m. This is the tricky part  we need a function that returns the accumulated number of days in the previous months since March, i.e. something like this: Code: month #days Example: at the beginning of month 5 (May) there were 61 days in previous months (31 in March plus 30 in April). Note that #days adds up only months with 30 or 31 days because February is the last month of the year, so the last added days (306 to 337) are are the 31 in January. This is a nearly linear function, so a straight line can be interpolated: Code: month 30,6*m  91,4 The interpolation line's slope of 30,6 equals 153/5. This is because the number of days sequence consists of two groups of five months with 31–30–31–30–31 days, i.e. 153 altogether. Which means an average of 30,6 days per month. Note the 153 and 306 days in the table. Now simply take the integer part of the right column and the result is exactly what we want. So the days from 1 March to the first day of month m are int(30,6*m  91,4) = int(30,6*m + 30,6 – 122) = int(30,6*(m+1)) – 122. Now we got the number of days in previous years plus the day count until the first of month m. Finally add the day d and we're done. So the complete formula is day number = 365*y + int(y/4) + int(30,6*(m+1)) – 122 + d Since the constant 122 only determines from which day in the past we are counting, it can also be omitted: day number = 365*y + int(y/4) + int(30,6*(m+1)) + d or day number = int(365,25*y) + int(30,6*(m+1)) + d This is the simplified method that assumes every fourth year is a leap year. As it is true for the Julian calendar, or the Gregorian calendar between 1 Mar 1900 and 28 Feb 2100. For a correct implementation of the complete Gregorian calendar we have to add the 100year and 400yearexceptions: Subtract the century years that are no leap years, and finally add back the 400year cases that are. So the result is day number = 365*y + int(y/4) – int(y/100) + int(y/400) + int(30,6*(m+1)) + d or day number = int(365,25*y) – int(y/100) + int(y/400) + int(30,6*(m+1)) + d Or with integer arithmetics, where "div" denotes an integer division: day number = 365*y + y div 4 – y div 100 + y div 400 + (153*(m+1)) div 5 + d That's all. No magic, just simple math. ;) Dieter 

« Next Oldest  Next Newest »

User(s) browsing this thread: 1 Guest(s)