AMPscript DateTime Functions

AMPscript DateTime functions allow us to manipulate display format, perform math functions, and control email content using static, dynamic, and real-time values with:

  • Now(1) Campaign Sends
  • Now(1) Triggered Sends
  • DateAdd(1, 2, 3)
  • DateDiff(1, 2, 3)
  • DateParse(1, 2)
  • DatePart(1, 2)

Now(1) Function – Campaign Sends

The Now() function is used to capture the system DateTime at the time of send. The system uses Central Standard Time and does not adjust for Daylight Savings Time. Now() returns a dynamic date that will update when using in an email VAWP (View As Web Page) link and or landing pages. You’ll want to use Now(1) to preserve the date at the time of send.

Example
%%=Now(1)=%%

<!-- Example Code -->
%%[
/**
* Now Logic
* Used to generate a dynamic date at the time of send
*/
VAR @date
SET @date = Now(1)
]%%
<p>%%=v(@date)=%%</p>
<!-- Example Output -->
12/14/2021 9:02:30 AM
View this gist on GitHub

Now(1) Function – Triggered Sends

Now(1) doesn’t work as expected for triggered sends. When used in a triggered send, it returns the last time the triggered-send-definition was published. If you need to maintain the accurate send time and date in a trigger (i.e. for VAWP links,) use this workaround: Add a date field to the triggered DE (name it something like “DateAdded”) and auto-populate it with the current date. This sets that field to the date/time the record was added to the DE (the date/time the triggered email was sent).

<!-- Example Code -->
%%[
/**
* Now Logic (Triggered Send Workaround)
* Using Now(1) in a triggered send returns the latest TSD published time.
* Add a date field to your triggered DE and name it something like DateAdded.
* Add a default value that auto-populates that new field with current date.
* This captures the date/time the subscriber was added to the triggered DE.
* Use a simple lookup that returns the DateAdded for that subscriber.
*/
VAR @dateRow, @sendDate
SET @dateRow = LookupRows("Your Trigger DE Name Here", "EmailAddress", @emailAddress)
SET @sendDate = Field(@dateRow, "DateAdded")
]%%
<p>%%=v(@sendDate)=%%</p>
<!-- Example Output -->
12/14/2021 9:02:30 AM
View this gist on GitHub

Date Add Function

The DateAdd() function is used to increase or decrease the date time value. It can be used with a date object or with the Now() function. You need to date objects in order for this to work.

Example
%%=DateAdd(1, 2, 3)=%%

<!-- Example Code -->
%%[
/**
* Date Add Logic
* Used to increase or decrease date values.
*/
VAR @initialDate, @previousDay, @nextDay, @nextMonth, @nextMinute, @nextHour
SET @initialDate = "12/14/2021 9:00:00 AM"
SET @previousDay = DateAdd(@initialDate,"-1","d") /* Decreases by one day */
SET @nextDay = DateAdd(@initialDate,"1","d") /* Increases by one day */
SET @nextMonth = DateAdd(@initialDate,"1","m") /* Increases by one month*/
SET @nextYear = DateAdd(@initialDate,"1","y") /* Increases by one year*/
SET @nextMinute = DateAdd(@initialDate,"1","mi") /* Increases by one minute */
SET @nextHour = DateAdd(@initialDate,"1","h") /* Increases by one hour */
]%%
<p>Initial Date: %%=v(@initialDate)=%%</p>
<p>Previous Day: %%=v(@previousDay)=%%</p>
<p>Next Day: %%=v(@nextDay)=%%</p>
<p>Next Month: %%=v(@nextMonth)=%%</p>
<p>Next Year: %%=v(@nextYear)=%%</p>
<p>Next Minute: %%=v(@nextMinute)=%%</p>
<p>Next Hour: %%=v(@nextHour)=%%</p>
<!-- Example Output -->
Initial Date: 12/14/2021 9:00:00 AM
Previous Day: 12/13/2021 9:00:00 AM
Next Day: 12/15/2021 9:00:00 AM
Next Month: 1/14/2022 9:00:00 AM
Next Year: 12/14/2022 9:00:00 AM
Next Minute: 12/14/2021 9:01:00 AM
Next Hour: 12/14/2021 10:00:00 AM
View this gist on GitHub

Date Diff Function

The DateDiff() function returns the difference between two date values. Always use DateDiff() when comparing DateTime values otherwise they will be compared as string values.

Example
%%=DateDiff(1, 2, 3)=%%

<!-- Example Code -->
%%[
/**
* Date Diff Logic
* Used to return the difference between two date values.
* Always use the DateDiff() when comparing DateTime values otherwise they will be compared as strings.
*/
VAR @date1, @date2, @dateDiff_Days, @dateDiff_Months, @dateDiff_Years, @dateDiff_Minutes, @dateDiff_Hours
SET @date1 = "12/14/2021 9:00:00 AM"
SET @date2 = "12/14/2022 9:00:00 AM"
SET @dateDiff_Days = DateDiff(@date1,@date2, "d")
SET @dateDiff_Months = DateDiff(@date1,@date2, "m")
SET @dateDiff_Years = DateDiff(@date1,@date2, "y")
SET @dateDiff_Minutes = DateDiff(@date1,@date2, "mi")
SET @dateDiff_Hours = DateDiff(@date1,@date2, "h")
]%%
<p>Date 1: %%=v(@date1)=%%</p>
<p>Date 2: %%=v(@date2)=%%</p>
<p>Difference in Days: %%=v(@dateDiff_Days)=%%</p>
<p>Difference in Months: %%=v(@dateDiff_Months)=%%</p>
<p>Difference in Years: %%=v(@dateDiff_Years)=%%</p>
<p>Difference in Minutes: %%=v(@dateDiff_Minutes)=%%</p>
<p>Difference in Hours: %%=v(@dateDiff_Hours)=%%</p>
<!-- Example Output -->
Date 1: 12/14/2021 9:00:00 AM
Date 2: 12/14/2022 9:00:00 AM
Difference in Days: 365
Difference in Months: 12
Difference in Years: 1
Difference in Minutes: 525600
Difference in Hours: 8760
View this gist on GitHub

Date Parse Function

The DateParse() function is used to convert a string into a DateTime object. Date might be provided as a string in the incorrect format so DateParse() converts it into a DateTime object in the proper format.

Example
%%=DateParse(1, 2)=%%

<!-- Example Code -->
%%[
/**
* Date Parse Logic
* Used to convert a string into a DateTime object.
* Date might be provided as a string in the incorect format so DateParse() converts it into a DateTime object in the proper format.
*/
VAR @date1, @date2, @date3, @date4, @date5
SET @date1 = DateParse("12/14/2021 9:00:00 AM")
SET @date2 = DateParse("December 14, 2017 3:40PM",1)
SET @date3 = DateParse("22:11:44 12/14/2017")
SET @date4 = DateParse("14:22:33 14 December 2017")
]%%
<p>%%=v(@date1)=%%</p>
<p>%%=v(@date2)=%%</p>
<p>%%=v(@date3)=%%</p>
<p>%%=v(@date4)=%%</p>
<!-- Example Output -->
12/14/2021 9:00:00 AM
12/14/2017 9:40:00 PM
12/14/2017 10:11:44 PM
12/14/2017 2:22:33 PM
View this gist on GitHub

Date Part Function

The DatePart() function isolates specific elements of a date object. This function is useful when having to break apart a date to format it around specific business requirements. It’s especially helpful converting dates to support other languages.

Example
%%=DatePart(1, 2)=%%

<!-- Example Code -->
%%[
/**
* Date Part Logic
* Used to isolate specific elements of a date object.
*/
VAR @date, @date_day, @date_month, @date_year, @date_minutes, @date_hours
SET @date = "12/14/2021 9:30:00 AM"
SET @date_day = DatePart(@date,"d")
SET @date_month = DatePart(@date,"m")
SET @date_year = DatePart(@date,"y")
SET @date_minutes = DatePart(@date,"mi")
SET @date_hours = DatePart(@date,"h")
]%%
<p>Date: %%=v(@date)=%%</p>
<p>Day: %%=v(@date_day)=%%</p>
<p>Month: %%=v(@date_month)=%%</p>
<p>Year: %%=v(@date_year)=%%</p>
<p>Minutes: %%=v(@date_minutes)=%%</p>
<p>Hours: %%=v(@date_hours)=%%</p>
<!-- Example Output -->
Date: 12/14/2021 9:30:00 AM
Day: 14
Month: 12
Year: 2021
Minutes: 30
Hours: 9
View this gist on GitHub

Gallery