The Format function changes the string to a specified format such as a date format, a currency format, or even a phone number.
For example, if the data attribute contains a date and time format in a number format and we want to have the date in a more readable text, the Format function would work well for this use case.
Use Case: Date
Syntax:
Format(1,2,3,4)
1 = String value to format (can be a variable)
2 = The format string to use (C# compatible)
3 = (Optional) Additional data format for value – “Date” or “Number”
4 = (Optional) Culture code – “en-US”
Example:
%%[
var @date
set @date = “02/14/2021 07:45 AM”
set @formatDate = Format(@date, “MMMM dd, yyyy”)
]%%
Output: %%=v(@formatDate)=%%
Output:
Output: February 14, 2021
Explanation:
The Format function evaluates the string variable @date with the value of “02/14/2021 07:45 AM” To make this date more readable, the Format function can be used to format the month of 02 into February and into a more readable date format of MMM dd, yyyy. For the population across the pond, this format can be updated to dd MMMM yyyy. See possible date formats below:
| Year (i.e. 2021) | Month (i.e. February) | Day (i.e. 14) |
| y = 21 | M = 2 | d = 14 |
| yy = 21 | MM = 02 | dd = 14 |
| yyy = 2021 | MMM = Feb | ddd = Sun |
| yyyy = 2021 | MMMM = February | dddd = Sunday |
Along with dates, the time can be formatted in Hours, Minutes, Seconds, AM/PM and an Offset time can be used for DateTimeOffset.
Use Case: Time
Example:
%%[
var @date
set @date = “02/14/2021 07:45 AM”
set @formatTime = Format(@date, “h:mm tt”, “Date”, “en-US”)
]%%
Output: %%=v(@formatTime)=%%
Output:
Output: 7:45 AM
Explanation:
The Format function evaluates the string variable @date with the value of “02/14/2021 07:45 AM” To make this time more readable, we remove the leading zero in the time and use the format string of h:mm tt. In this example, we are using the options 3rd and 4th parameters of Date and the country code of en-US. Below are the time formats available.
| Hours (i.e. 7) | Minutes (i.e. 45) | Seconds (i.e. 01) | AM/PM (i.e. AM) | Offset (i.e. -6) |
| h = 7 | m = 45 | s = 1 | t = A | z = -6 |
| hh = 07 | mm = 45 | ss = 01 | tt = AM | zz = -06 |
| H = 7 | – | – | – | zzz = -06:00 |
| HH = 7 (uses 24 hour clock) | – | – | – | – |
Use Case: Currency
In addition to Date and Time, another common use case is the formatting of currency. Given a decimal value in the data attribute, the output can be formatted to a dollar amount rounded to the cent or dollar place.
Example:
%%[
var @amount
set @amount = “54.32322”
set @formatAmount = Format(@amount, “$#,#.00;-$#,#.00”)
]%%
Output: %%=v(@formatAmount)=%%
Output:
Output: $54.32
Explanation:
The Format function evaluates the string variable @amount with the value of “54.32322” To make this value a more readable currency with a rounded dollar amount, use the format string of h$#,#.00;-$#,#.00.
Use Case: Phone
Lastly, a common use case for Format that I’ve seen used is to format a phone number. Oftentimes, the phone data attribute may come in different format values depending on the data source and how the phone number is collected. To keep a consistent phone format value, the Format function can be used to separate the area code or prefix from the rest of the phone number.
Example:
%%[
var @phone
set @phone = “3235553232”
set @formatPhone = Format(@phone, “(###) ###-####”)
]%%
Output: %%=v(@formatPhone)=%%
Output:
Output: (323) 555-3232
Explanation:
The Format function evaluates the string variable @phone with the value of “3235553232” Oftentimes, the phone number value is received in different formats. Some with leading country codes and some with just 10 digits. When used in conjunction with the IsPhoneNumber function, the Format function can be used to properly display the phone number. To make this value a more readable phone with an area code, use the format string of (###) ###-####.
Thank you!