Difference Between Dates and Optional Times Date 1:   ±Y  M  D Time 1:   H M S Calendar Mode:   Julian            Gregorian Date 2:   ±Y  M  D Time 2:   H M S
 ```DATE1 ISOymd1 = 20170623 = 2017 June 23 = Friday TIME1 HMS = 12 00 00.000 = 0.5 day = TimeFrac1 JD1 = 2457928 = JD for Date 1 and Time 1 CalMode = 1 (Gregorian) -------------------- 2017 June Su Mo Tu We Th Fr Sa              1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 -------------------- DATE2 ISOymd2 = 20170623 = 2017 June 23 = Friday TIME2 HMS = 12 00 00.000 = 0.5 day = TimeFrac2 JD2 = 2457928 = JD for Date 2 and Time 2 CalMode = 1 (Gregorian) -------------------- 2017 June Su Mo Tu We Th Fr Sa              1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 -------------------- DIFFERENCE BETWEEN DATES AND TIMES = (JD2 - JD1) = 0 day(s) = 0 Gregorian yr = ``` This program uses a cookie to remember the settings between calls.

 Given any two dates on the same calendar system, starting date (ymdA) and ending date (ymdB), to compute the difference between the dates, we simply compute their respective JD numbers (JDA and JDB) and take the difference between them. There are two different types of difference.  The simple difference between dates equates to an integer number of days between any two dates.  The second type of difference is the more precise difference that also includes the time of day of each of the dates as part of the computation and results in a non-integer, fractional JD value. NOTE: Remember that we are using calendrical JD numbers here.  For astronomical application, we would need to subtract 0.5 from the calendrical JD values.  However, for the simple purpose of computing the difference between calendrical dates and times, we do not need to use the strict astronomical JD value, so the step of subrtacting 0.5 has been eliminated from the JD computation in this case.  It would be redundant, since the difference, the single focus of the computation, works out to the same value anyway. The following algorithm computes the difference in whole days between any two calendar dates, the only requirements being that both dates be valid and on the same calendar system.  This is the algorithm to apply when the time of day can be ignored.  In this simplest case, the difference will always equate to a positive or negative integer value, depending on the choice of starting and ending dates. Algorithm 4: Compute the difference in whole days between any two integer encoded calendar dates(ISOymdA, ISOymdB) and common calendar mode (CalMode). ``` JDA = JD_For_ISOymd (ISOymdA, CalMode) JDB = JD_For_ISOymd (ISOymdB, CalMode) DiffDays = JDB - JDA ``` ACCOUNTING FOR THE TIME OF DAY If we also wish to take the time of day into account when computing the difference, the problem becomes a little more complicated and involves the use of non-integer or fractional JD number values and the conversion of time elements into decimal day fractions and vice versa.  However, these extra complications are very easy to handle. The time elements are: hh = Hours (00 to 24) mm = Minutes (00 to 59) ss = Seconds (00 to 59) DayFrac = Fraction of a day corresponding to the time elements. Computing the fraction of the day corresponding to the time elements is done by converting the time of day into the equivalent number of seconds since the day began (since last midnight) and then dividing by the number of seconds in one day or 24 hours (86400 seconds).  This results in a fractional value between 0 and 1 which is added to the simple integer JD number for the date to obtain the general JD number corresponding to the given date and time. The following algorithm is the one to apply when we also need to take the time of day into account as part of the computation.  Given the general calendrical JD values for two dates and times, we simply take the difference between them, except in this case, the difference (DiffDays) value may equate to a non-integer value.  Algorithm 5: Compute the difference in decimal days between any two calendar dates and times and common calendar mode. ``` JDA = JD_For_ISOymd (ISOymdA, CalMode) + (3600*hhA + 60*mmA + ssA)/86400 JDB = JD_For_ISOymd (ISOymdB, CalMode) + (3600*hhB + 60*mmB + ssB)/86400 DiffDays = JDB - JDA ``` The DiffDays value computed by the above algorithm will generally be a fractional value if there are any non-zero time elements and can be a positive or negative value, depending on the choice of starting and ending points. Once we compute the decimal DiffDays value, we want to express it in terms of the equivalent standard time units.  This is essentially the opposite of converting the date elements into a day fraction.  Here we are converting a decimal days value into the corresponding time elements. Since the difference may be a large number of days, we will break it down into descending time units of weeks (wk), days (d), hours (hh), minutes (mm) and seconds (ss). For the purpose of computing the difference, we will work with the absolute value of the difference when computing the time elements.  The numerical sign can be remembered and reattached to the computed result if necessary. Algorithm 6: Compute descending time elements:weeks (wk), days (d), hours (hh), minutes (mm) and seconds (ss), from the absolute difference between the JD values of the respective dates and times. ``` DiffDays = abs(JDB - JDA) wk = floor(DiffDays / 7) days = DiffDays - 7*wk d = floor(days) hours = 24*(days - d) hh = floor(hours) minutes = 60*(hours - hh) mm = floor(minutes) seconds = 60*(minutes - mm) ss = floor(seconds + 0.5) ``` The computational resolution used here is to the nearest second, which should be more than sufficient. EXAMPLE: Let`DiffDays = 11.649600735979` The task is to break this value down into the standard time elements: weeks (`wk`), days (`d`), hours (`hh`), minutes (`mm`) and seconds (`ss`). Given the difference value, Algorithm 6 yields the following corresponding time elements. ``` DiffDays = 11.649600735979 From which we compute: w = abs(11.649600735979) = 11.649600736 wk = floor(11.649600736 / 7) = 1 days = 11.649600736 - 7*1 = 4.64960073598 d = floor(4.64960073598) = 4 hours = 24*(4.64960073598 - 4) = 15.5904176635 hh = floor(15.5904176635) = 15 minutes = 60*(15.5904176635 - 15) = 35.4250598098 mm = floor(35.4250598098) = 35 seconds = 60*(35.4250598098 - 35) = 25.5035885856 ss = floor(25.5035885856 + 0.5) = 26 So, a difference of 11.649600735979 days equates to: 1 week 4 days 15h 35m 26s when resolved to the nearest second. ``` ...

© Jay Tanner - PHP Science Labs - 2017