AMPscript Date and Time Formatting
AMPscript assumes all date datatype values use en-US or UTC formatting. You can output several different date and time formats with AMPscript, depending on your needs.
We recommend using the Format() function for date and time formatting that requires a locale setting.
This table shows available formats for both the Format() and FormatDate() functions, illustrated by the subsequent examples. For a longer table of available tokens and formats, review AMPscript Date and Time Formats: Format() and FormatDate().
This table demonstrates the available formats:
Year (Example: 2011) | Month (Example: August) | Date (Example: Saturday, August 06) | Hours (Example: 20) | Minutes (Example: :00) | Seconds (Example: :05) | AM/PM (Example: PM) | Offset (Example: -6:00) |
---|---|---|---|---|---|---|---|
y = 11 | M = 8 | d = 6 | h = 8 | m = 0 | s = 5 | t = P | z = -6 |
yy = 11 | MM = 08 | dd = 06 | hh = 08 | mm = 00 | ss = 05 | tt = PM | zz = -06 |
yyy = 2011 | MMM = Aug | dddd = Sat | H = 8 | - | - | - | zzz = -06:00 |
yyyy = 2011 | MMMM = August | ddddd = Saturday | HH = 20 | - | - | - | - |
The output for M varies depending on whether M is used in a single function or within a group of functions.
The M variable in a single function displays as a numeral.
As a single M within a group of functions, the result is the full name of the month and the date in two-digit format. If you use the % character with the M variable in a group of functions, the M variable displays output as if it were in a single function.
There are no functions to set the timezone
property of the dateTime value.
- The
Format()
function doesn't specify timezones. - Choosing a Locale does not affect the timezone.
- The use of
zzz
is not useful in the context of timezones, because thetimezone
value is always the server timezone.
The customer can calculate the dateTime value for a different offset, or use the SystemDateToLocalDate()
function.
The next example takes the results of the Now() function, and returns three different formats.
or
Result:
This example takes the results of the Now() function and returns it in the specified format.
%%=Format(Now(), "MMMM d, yyyy")=%%
or
%%=FormatDate(Now(), "MMMM d, yyyy")=%%
Result: August 15, 2011
This example formats a date and returns it in the specified format:
%%=Format("2011-08-15 20:00:05.567890", "MMM dd, yyyy h:mm:ss.tt", "Date", "en-US")=%%
or
%%=FormatDate("2011-08-15 20:00:05.567890", "MMM dd, yyyy", "l", "en-US")=%%
Result: Aug 15, 2011 8:00:05.PM
This example takes the value of Now(), offsets it using the DateAdd() function, and then returns that value in the specified format.
or
Given that the date is August 15, 2011
, The output is: Date: August 26, 2011
.
This example outputs the Birthday attribute of a subscriber, from all subs or the sendable data extension in the specified format.
%%[Output(Format([Birthday],"dd/MM/yyyy"))]%%
Given a Birthday attribute equal to August 15, 2011, The output is: 15/08/2011