AMPscript – DateAdd()

Sometimes the date you have is not the date you need. With AMPscript you can go forward or pull dates before. The DateAdd() function is what you would use. A positive date adds dates and a negative number would subtract days from the current date.

One common use case is setting up date comparisons. (Remember if you are looking for days before something, you will want to use “<” the date. )

DateAdd() doesn’t just work for days, you can also add/subtract Years and Months. It also works for datetime adding/subtracting hour and minutes.


SYNTAX

DateAdd(Date, Number, Type)

Definition of parts

Date = The date you have, in MM/dd/yyyy or YYYY-MM-DD format

Number = How many you want to add. Remember if you want to subtract days you would just put an negative number here instead.

Type = Year (Y), Month (M), Day (D), Hour (H), Minute (MI)


Simple Examples:

Tomorrow → DateAdd(‘2019-12-21’, 1, D)
Yesterday → DateAdd(‘2019-12-21’, -1, D)
A week from today → DateAdd(’10/21/2019′,7,D)
2 weeks ago → DateAdd(’10/21/2019′,-14,D)


Advanced Example:

Labeling members based on member tenure. Passing the sign-up date over in the audience DE. One use for this would be to have conditional statements in your email to show certain things to people based on what tenure group.

%%[
 VAR @signupdate, @tenure
 set @signupdate = [Signup Date]
 IF @signupdate <= DateAdd(NOW(), '-60','D') THEN 
   set @tenure = 'over-60-day'
 ELSEIF @signupdate <= DateAdd(NOW(), '-30','D') THEN
   set @tenure = '30-to-60-day'
 ELSE 
   set @tenure = 'less-than-30-day'
 ENDIF 
 ]%%

References:

Salesforce Developer – DateAdd()

AMPscript Guide – DateAdd()

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.