Hi McUsrII.
Both methods appear to get the same results with dates as far apart as 1st January 0001 and 31st December 9999. But the div/mod method should explicity coerce the seconds result to integer in case the difference really is that large .
-- date "31 December 9999 00:00:00"
set endDate to (current date)
tell endDate to set {its day, its year, its month, its day, its time} to {1, 9999, December, 31, 0}
-- date "1 January 0001 23:59:59" (proleptic Gregorian)
copy endDate to startDate
tell startDate to set {its day, its year, its month, its time} to {1, 1, January, days - 1}
if (startDate > endDate) then error "I can't have that start date is after end date!"
set timeDiff to endDate - startDate
set hourDiff to timeDiff div hours
set minDiff to timeDiff mod hours div minutes
set secDiff to timeDiff mod minutes as integer
{hourDiff, minDiff, secDiff}
--> {87649368, 0, 1}
. and for accuracy, yours should perform the “borrow” operations from the seconds up rather than from the days down:
-- date "31 December 9999 00:00:00"
set endDate to (current date)
tell endDate to set {its day, its year, its month, its day, its time} to {1, 9999, December, 31, 0}
-- date "1 January 0001 23:59:59" (proleptic Gregorian)
copy endDate to startDate
tell startDate to set {its day, its year, its month, its time} to {1, 1, January, days - 1}
if (startDate > endDate) then error "I can't have that start date is after end date!"
set dayDiff to ((endDate - (time of endDate)) - (startDate - (time of startDate))) div days
set hourDiff to dayDiff * 24 + (hours of endDate) - (hours of startDate)
set minDiff to (minutes of endDate) - (minutes of startDate)
set secDiff to (seconds of endDate) - (seconds of startDate)
if (secDiff < 0) then set {secDiff, minDiff} to {secDiff + 60, minDiff - 1}
if (minDiff < 0) then set {minDiff, hourDiff} to {minDiff + 60, hourDiff - 1}
{hourDiff, minDiff, secDiff}
--> {87649368, 0, 1}
In both cases, it should be noted that AppleScript has a long-standing bug whereby the string representations of dates earlier than approximately 1 January 1848 00:00:00 are out by a number of seconds depending on whereabouts in the world you are, so such dates must be created by setting date properties rather than using a date specifier.
Also, of course, something other than ‘text -1 thru -2 of (“0” & hourDiff)’ should be used if the hours figure’s likely to exceed 99.