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)=%%
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).
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)=%%
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)=%%
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)=%%
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)=%%
Hi Everyone! I hope this information helps. You can download the full code using the links below. Feel free to modify it to fit your needs. I’d love to get your feedback, did this information help in some way?
AMPscript Now Code (Campaign Email)
https://gist.github.com/nmaratasdev/67e4b8edb8f22353edfed69fab384c5b
AMPscript Now Code (Triggered Send Email)
https://gist.github.com/nmaratasdev/c5c5ae33e3ac3f0ad32c4ccd6e5a9f54
AMPscript Date Add Code
https://gist.github.com/nmaratasdev/3e4d8b09f74ee2c6ca8ffb017b5afa5d
AMPscript Date Diff Code
https://gist.github.com/nmaratasdev/5f01b4884b7feabdf6d17c6f2cc5bc76
AMPscript Date Parse Code
https://gist.github.com/nmaratasdev/cfffd9828d0df5397df438cac54cee0f
AMPscript Date Part Code
https://gist.github.com/nmaratasdev/a5f10cffcff619f4f9f8ef722adc30aa