Function Reference


_DateAdd

Calculates a new date/time by adding/subtracting a specified number of time intervals from an initial date/time

#include <Date.au3>
_DateAdd ( $sType, $vNumber, $sDate )

Parameters

$sType Time interval to be used:
D - Add/subtract days to/from the specified date
M - Add/subtract months to/from the specified date
Y - Add/subtract years to/from the specified date
w - Add/subtract Weeks to/from the specified date
h - Add/subtract hours to/from the specified date
n - Add/subtract minutes to/from the specified date
s - Add/subtract seconds to/from the specified date
a - Add/subtract [days, hours, minutes, seconds] to/from the specified date
$vNumber Number of intervals to be added/subtracted (use unary minus for subtraction)
or an $vNumber[4] for values to be added/substracted to [days, hours, minutes, seconds]
$sDate Initial date in the format YYYY/MM/DD[ HH:MM:SS]

Return Value

Success: Calculated date.
Failure: 0 and sets the @error flag to non-zero.
@error: 1 - Invalid $sType
2 - Invalid $iNumber
3 - Invalid $sDate

Remarks

Valid initial date must be between "2000/01/01 00:00:00". and "3000/12/31 23:59:59".

The function will not return an invalid date. For example, if 3 months are added to '2004/1/31' then the result will be '2004/04/30'.

See _DateTimeSplit() for other possible variations of the input date format.

Related

_DateDiff, _DateTimeSplit, _DateToDayOfWeek, _DateToDayOfWeekISO, _DateToDayValue, _DayValueToDate

Example

Example 1

#include <Date.au3>
#include <MsgBoxConstants.au3>

; Add 5 days to today
Local $sNewDate = _DateAdd('d', 5, _NowCalcDate())
MsgBox($MB_SYSTEMMODAL, "", "Today + 5 days: " & $sNewDate)

; Subtract 2 weeks from today
$sNewDate = _DateAdd('w', -2, _NowCalcDate())
MsgBox($MB_SYSTEMMODAL, "", "Today minus 2 weeks: " & $sNewDate)

; Add 15 minutes to current time
$sNewDate = _DateAdd('n', 15, _NowCalc())
MsgBox($MB_SYSTEMMODAL, "", "Current time +15 minutes: " & $sNewDate)

; Calculated eventlogdate which returns second since 1970/01/01 00:00:00
$sNewDate = _DateAdd('s', 1087497645, "1970/01/01 00:00:00")
MsgBox($MB_SYSTEMMODAL, "", "Date: " & $sNewDate)

Sample 2 script using $sType = "a"

#include <Date.au3>
#include <MsgBoxConstants.au3>

Local $sStartDate = "2024/08/19 18:25:59"
Local $sEndDate = "2024/08/20 16:25:00"

Local $aDiffDate = _DateDiff("a", $sStartDate, $sEndDate)

Local $sAddDate = _DateAdd("a", $aDiffDate, $sStartDate)

Local $iError = 0
If $sAddDate <> $sEndDate Then $iError = $MB_ICONERROR
MsgBox($MB_SYSTEMMODAL + $iError, "Results", "$sStartDate = " & $sStartDate & @CRLF & _
        "$sEndDate = " & $sEndDate & @CRLF & @CRLF & _
        "$Diff Days = " & $aDiffDate[0] & @CRLF & _
        "$Diff Hours = " & $aDiffDate[1] & @CRLF & _
        "$Diff Minutes = " & $aDiffDate[2] & @CRLF & _
        "$Diff Seconds = " & $aDiffDate[3] & @CRLF & @CRLF & _
        "Add Date = " & $sAddDate & @TAB & ($iError ? "<<< KO >>>": "OK"))