$out1 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:
and common calendar mode.
Compute the difference in decimal days between any two calendar dates and times 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 = $RandDays`
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.
...
$out2 |