DateDiff()

Auto Hotkey

DateDiff

Compares two date-time values and returns the difference.

OutputVar := DateDiff(DateTime1, DateTime2, TimeUnits)
Function Example: Days := DateDiff("20100101", A_Now, "days")

Parameters

DateTime1,
DateTime2

Date-time stamps in the YYYYMMDDHH24MISS format.

If DateTime1 is earlier than DateTime2, the result is a negative number.

TimeUnits
Units to measure the difference in. TimeUnits may be one of the following strings (or just the first letter):
Seconds, Minutes, Hours or Days.

Return Value

If DateTime contains an invalid timestamp or a year prior to 1601, an empty string is returned to indicate the error.

Otherwise, the return value is the difference between the two timestamps, in the units specified by TimeUnits. The result is always rounded down to the nearest integer. For example, if the actual difference between two timestamps is 1.999 days, it will be reported as 1 day. If higher precision is needed, specify Seconds for TimeUnits and divide the result by 60.0, 3600.0, or 86400.0.

Remarks

The built-in variable A_Now contains the current local time in YYYYMMDDHH24MISS format.

To precisely determine the elapsed time between two events, use the A_TickCount method because it provides millisecond precision.

To add or subtract a certain number of seconds, minutes, hours, or days from a timestamp, use DateAdd (subtraction is achieved by adding a negative number).

Related

DateAdd, FileGetTime, FormatTime

Example

var1 := "20050126"
var2 := "20040126"
diff := DateDiff(var1, var2, "days")
MsgBox %diff%  ; The answer will be 366 since 2004 is a leap year.