Skip to main content

dateTimeDifference

Returns the absolute difference in seconds between two datetime objects (or strings in an automatically recognized datetime pattern). The result is always positive and can include a fractional part.

If both input parameters have a timezone offset, then the offsets are included in the difference calculation. If only one parameter has an offset, the offset is ignored.

If an input is missing the time component, the default time is taken as midnight (00:00:00), which is displayed in your project output text as 12:00:00 AM (the default US formatting).

Parameters

  • DATETIME1 (datetime or string)

    The first datetime object/string.

  • DATETIME2 (datetime or string)

    The second datetime object/string.

Examples

For simplicity, these examples use strings in an automatically recognized datetime pattern.

ATL in Script

Result

Notes

[[
time1 = '00:00:00.5';
time2 = '00:00:00.6';
dateTimeDifference(time1, time2)
]]

0.1

The input values are time-only strings that are different by one tenth of a second.

[[
time1 = '00:00';
time2 = '00:10';
dateTimeDifference(time1, time2)
]]

600

The input values are time-only strings that are different by 10 minutes. This duration is returned in seconds.

[[
date1 = '2018-12-12';
date2 = '2018-12-10';
dateTimeDifference(date1, date2)
]]

172,800

The input values are date-only strings that are different by two days. This duration is returned in seconds.

[[
dateTime1 = '2010-01-02T13:00:00';
dateTime2 = '2010-01-01T13:00:00';
dateTimeDifference(dateTime1, dateTime2)
]]

86,400

The input values are date-and-time strings that are different by one day. This duration is returned in seconds.

[[
dateVal = '2018-12-12';
dateTimeVal = '2018-12-12T00:00:05';
dateTimeDifference(dateVal, dateTimeVal)
]]

5

Only one input value has a time component. When an input is date-only, the time is assumed to be midnight.

[[
dateTime1 = '2010-01-01T13:00:00+10:00';
dateTime2 = '2010-01-02T13:00:00+00:00';
dateTimeDifference(dateTime1, dateTime2)
]] 

122,400

Both input values are date-and-time strings including a timezone offset. Therefore, the offset is included in the result.

[[
dateTime1 = '2010-01-01T13:00:00+10:00';
dateTime2 = '2010-01-02T13:00:00';
dateTimeDifference(dateTime1, dateTime2)
]]

86,400

Both input values are date-and-time strings but only one includes a timezone offset. Therefore, the offset is ignored.

Note

The function always returns the difference in seconds. You can use other ATL functions to convert the result to a different time unit — see Datetime functions for guidance.