AMPscript Format Functions

AMPscript DateTime functions allow us to manipulate display format, perform math functions, and control email content using static, dynamic, and real-time values. Formatting functions provide a powerful tool set for converting various data types to formatted strings and numbers.

  • FormatDate(1, 2, 3, 4)
  • FormatCurrency(1, 2, 3, 4)
  • FormatNumber(1, 2, 3)
  • Format(1, 2, 3, 4)

Format Date Function

This function formats a specified string as a date value for a specified format pattern and locale.

Example
%%=FormatDate(1, 2, 3, 4)=%%

<!-- Example Code -->
%%[
/**
* Format Date Logic
* Formats a specified string as a date value for a specified format and locale.
*/
VAR @date, @date_english, @date_spanish, @date_french, @date_chinese
SET @date = "2012-10-05 03:30:34.567890"
SET @date_english = FormatDate(@date, "l", "HH:MM tt", "en-us")
SET @date_spanish = FormatDate(@date, "l", "HH:MM tt", "es-mx")
SET @date_french = FormatDate(@date, "l", "HH:MM tt", "fr-fr")
SET @date_chinese = FormatDate(@date, "l", "HH:MM tt", "zh-cn")
]%%
<p>English: %%=v(@date_english)=%%</p>
<p>Spanish: %%=v(@date_spanish)=%%</p>
<p>French: %%=v(@date_french)=%%</p>
<p>Chinese: %%=v(@date_chinese)=%%</p>
<!-- Example Output -->
English: Friday, October 5, 2012 03:30 AM
Spanish: viernes, 5 de octubre de 2012 03:30 a. m.
French: vendredi 5 octobre 2012 03:30
Chinese: 2012年10月5日 03:30 上午
View this gist on GitHub

Format Currency Function

This function formats a specified string as a currency value for a specified locale.

Example
%%=FormatCurrency(1, 2, 3, 4)=%%

<!-- Example Code -->
%%[
/**
* Format Currency Logic
* Formats a specified string as a currency value for a specified format and locale.
*/
VAR @price, @price_usa, @price_mexico, @price_france, @price_canada, @price_china
SET @price = "199.99"
SET @price_usa = FormatCurrency(@price, "en-us")
SET @price_mexico = FormatCurrency(@price, "es-mx")
SET @price_france = FormatCurrency(@price, "fr-fr")
SET @price_canada = FormatCurrency(@price, "fr-ca")
SET @price_china = FormatCurrency(@price, "zh-cn")
]%%
<p>USA: %%=v(@price_usa)=%%</p>
<p>Mexico: %%=v(@price_mexico)=%%</p>
<p>France: %%=v(@price_france)=%%</p>
<p>Canada: %%=v(@price_canada)=%%</p>
<p>China: %%=v(@price_china)=%%</p>
<!-- Example Output -->
USA: $199.99
Mexico: $199.99
France: 199,99 €
Canada: 199,99 $
China: ¥199.99
View this gist on GitHub

Format Number Function

This function formats a specified string as a number for a specified locale.

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

<!-- Example Code -->
%%[
/**
* Format Number Logic
* Formats a specified string as a number value for a specified format or locale.
*/
VAR @number, @currency_english, @currency_french, @decimal, @fixed, @percent, @general
SET @number = "199"
SET @currency_english = FormatNumber(@number, "C", "en-us")
SET @currency_french = FormatNumber(@number, "C", "fr-fr")
SET @decimal = FormatNumber(@number, "D")
SET @fixed = FormatNumber(@number, "F")
SET @percent = FormatNumber(@number, "P")
SET @general = FormatNumber(@number, "G")
]%%
<p>Currency (English): %%=v(@currency_english)=%%</p>
<p>Currency (French): %%=v(@currency_french)=%%</p>
<p>Decimal: %%=v(@decimal)=%%</p>
<p>Fixed: %%=v(@fixed)=%%</p>
<p>Percent: %%=v(@percent)=%%</p>
<p>General: %%=v(@general)=%%</p>
<!-- Example Output -->
Currency (English): $199.00
Currency (French): 199,00 €
Decimal: 199
Fixed: 199.00
Percent: 19,900.00%
General: 199
View this gist on GitHub

Format Function

This function formats a string into a specified format pattern and locale. You’ll want to use this function for date and time formatting that requires a locale setting.

Example
%%=Format(1, 2, 3, 4)=%%

<!-- Example Code -->
%%[
/**
* Format Logic
* This function formats a string into a specified format pattern and locale.
* Use this function for date and time formatting that requires a locale.
*/
VAR @date, @long_date_english, @long_date_spanish, @short_date_english, @short_date_spanish, @full_long_date_english, @full_long_date_spanish, @full_short_date_english, @full_short_date_spanish, @general_long_date_english, @general_long_date_spanish, @general_short_date_english, @general_short_date_spanish
SET @date = "2012-10-05 03:30:34.567890"
SET @long_date_english = Format(@date,"D", "Date", "en-us")
SET @long_date_spanish = Format(@date,"D", "Date", "es-mx")
SET @short_date_english = Format(@date,"d", "Date", "en-us")
SET @short_date_spanish = Format(@date,"d", "Date", "es-mx")
SET @full_long_date_english = Format(@date,"F", "Date", "en-us")
SET @full_long_date_spanish = Format(@date,"F", "Date", "es-mx")
SET @full_short_date_english = Format(@date,"f", "Date", "en-us")
SET @full_short_date_spanish = Format(@date,"f", "Date", "es-mx")
SET @general_long_date_english = Format(@date,"G", "Date", "en-us")
SET @general_long_date_spanish = Format(@date,"G", "Date", "es-mx")
SET @general_short_date_english = Format(@date,"g", "Date", "en-us")
SET @general_short_date_spanish = Format(@date,"g", "Date", "es-mx")
]%%
<p>Long Date (English): %%=v(@long_date_english)=%%</p>
<p>Long Date (Spanish): %%=v(@long_date_spanish)=%%</p>
<p>Short Date (English): %%=v(@short_date_english)=%%</p>
<p>Short Date (Spanish): %%=v(@short_date_spanish)=%%</p>
<p>Full Long Date (English): %%=v(@full_long_date_english)=%%</p>
<p>Full Long Date (Spanish): %%=v(@full_long_date_spanish)=%%</p>
<p>Full Short Date (English): %%=v(@full_short_date_english)=%%</p>
<p>Full Short Date (Spanish): %%=v(@full_short_date_spanish)=%%</p>
<p>General Long Date (English): %%=v(@general_long_date_english)=%%</p>
<p>General Long Date (Spanish): %%=v(@general_long_date_spanish)=%%</p>
<p>General Short Date (English): %%=v(@general_short_date_english)=%%</p>
<p>General Short Date (Spanish): %%=v(@general_short_date_spanish)=%%</p>
<!-- Example Output -->
Long Date (English): Friday, October 5, 2012
Long Date (Spanish): viernes, 5 de octubre de 2012
Short Date (English): 10/5/2012
Short Date (Spanish): 05/10/2012
Full Long Date (English): Friday, October 5, 2012 3:30:34 AM
Full Long Date (Spanish): viernes, 5 de octubre de 2012 03:30:34 a. m.
Full Short Date (English): Friday, October 5, 2012 3:30 AM
Full Short Date (Spanish): viernes, 5 de octubre de 2012 03:30 a. m.
General Long Date (English): 10/5/2012 3:30:34 AM
General Long Date (Spanish): 05/10/2012 03:30:34 a. m.
General Short Date (English): 10/5/2012 3:30 AM
General Short Date (Spanish): 05/10/2012 03:30 a. m.
View this gist on GitHub

Gallery