The DateAdd function returns the sum of an integer and a date value. In other words, a number can be added to a date to add days, months, or years, hours or minutes. A good use case is using the DateAdd function as a coupon expiration date. DateAdd can be used in conjunction with the Now function.
Syntax:
DateAdd(1,2,3)
1 = Date value to use to add an integer to (can be a variable). Valid values include ‘MM/dd/yyyy’ or ‘YYYY-MM-DD’
2 = Integer to add to the date in parameter 1
3 = Unit of time specified Valid values include ‘Y’ (year), ‘M’ (month), ‘D’ (day), ‘H’ (hour), ‘MI’ (minute)
Example:
%%[
var @date, @expirationDate
set @date = Now()
set @expirationDate = DateAdd(@date, “30”, “D”)
]%%
Expires on: %%=v(@expirationDate)=%%
Output:
Expires on: 3/16/2021 11:38:12 PM
The output also gives the current time which we may not want to display. This is a good opportunity to use the Format function to remove the time and make the date more readable
%%[
var @date, @expirationDate
set @date = Now()
set @expirationDate = Format(DateAdd(@date, “30”, “D”), “MMMM dd, yyyy”)
]%%
Expires on: %%=v(@expirationDate)=%%
Output:
Expires on: March 16, 2021
Explanation:
Using the Format function and DateAdd to calculate the today’s date (using the Now function) plus 30 days. This gives a human-readable output of a date that is 30 days from today’s date.