Handlebar Helper Function: Format

Formats output based on a format string and optional type specification. It supports formatting dates, numbers, and strings according to specified patterns and culture settings.

ParameterTypeDescription
subjectanyRequired. The value to format.
formatStringstringRequired. The format pattern to apply. See Format Strings.
typestringThe type to format as. Valid values: "date", "numeric". If not specified, uses general string formatting.
culturestringThe culture code for formatting, such as en-US or zh_TW. Supports both hyphen and underscore formats. Defaults to current runtime culture if not specified.

The value that you specify for the formatString parameter determines the type of formatting to apply to the subject. These sections describe the different format strings that you can use.

Date formats are highly configurable and can be customized to suit your needs. These sections describe the different format strings that you can use.

Use the string patterns listed in this table to format dates and times. The values in the Example Output column are based on the date Monday, March 15, 2024 at 2:30:45 PM in the UTC-6 time zone with the en_US culture.

StringPatternNameExample Output
dShort date3/15/2024
DLong dateFriday, March 15, 2024
fFull date/time (short)Friday, March 15, 2024 2:30 PM
FFull date/time (long)Friday, March 15, 2024 2:30:45 PM
gGeneral (short)3/15/2024 2:30 PM
GGeneral (long)3/15/2024 2:30:45 PM
MMonth dayMarch 15
ORound-trip2024-03-15T08:30:45.1230000-06:00
RRFC1123Fri, 15 Mar 2024 14:30:45 GMT
sSortable2024-03-15T08:30:45
tShort time2:30 PM
TLong time2:30:45 PM
uUniversal sortable2024-03-15 14:30:45Z
UUniversal full2024-03-15T14:30:45.0Z
YYear monthMarch 2024

Use the string patterns listed in this table to format dates. The values in the Example column are based on the date Monday, March 15, 2024 at 2:30:45 PM in the UTC-6 time zone with the en_US culture.

String PatternDescriptionExampleValid Range
yyyy4-digit year20240001–9999
yyy3-digit year (minimum)2024001–999
yy2-digit year2400–99
y1-digit year (minimum)20241–9999
MMMMFull month nameMarchCulture-dependent
MMMAbbreviated month nameMarCulture-dependent
MM2-digit month0301–12
MMonth without leading zero31–12
ddddFull day of weekFridayCulture-dependent
dddAbbreviated day of weekFriCulture-dependent
dd2-digit day1501–31
dDay without leading zero151–31

Use the string patterns listed in this table to format times. The values in the Example column are based on the time 2:30:45 PM in the UTC-6 time zone with the en_US culture.

String PatternDescriptionExampleValid Range
HH24-hour format (2-digit)1400–23
H24-hour format (1-digit min)140–23
hh12-hour format (2-digit)0201–12
h12-hour format (1-digit min)21–12
mmMinutes (2-digit)3000–59
mMinutes (1-digit min)300–59
ssSeconds (2-digit)4500–59
sSeconds (1-digit min)450–59
fffMilliseconds (3-digit)123000–999
ffCentiseconds (2-digit)1200–99
fDeciseconds (1-digit)10–9
ttAM/PM designatorPMCulture-dependent
tFirst char of AM/PMPCulture-dependent

You can further format dates and times using these literals and separators.

String PatternDescriptionExample
'text'Literal text (single quotes)'of'
"text"Literal text (double quotes)"the"
/Date separator/
:Time separator:
-Dash separator-
Space(space)

You can customize the way that numbers are formatted by using predefined or custom format strings.

Use the string patterns listed in this table to format numbers. The values in the Example columns are based on the en_US culture setting.

PatternNameExample (1234.567)Example (-1234.567)Notes
C or C2Currency$1,234.57($1,234.57)Uses the specified culture's currency symbol and formatting.
C0Currency (no decimals)$1,235($1,235)Rounds to nearest integer.
C4Currency (4 decimals)$1,234.5670($1,234.5670)Zero-padded to 4 decimal places.
DDecimal1235-1235Rounds to nearest integer.
D8Decimal (8 digits)00001235-00001235Left-padded with zeros to 8 digits.
E or E2Scientific1.23E+003-1.23E+003Exponential notation with an uppercase 'E'.
e3Scientific (lowercase)1.235e+003-1.235e+003Exponential notation with a lowercase 'e'.
F or F2Fixed-point1234.57-1234.57Doesn’t include a thousands separator.
F0Fixed-point (no decimals)1235-1235Rounds to nearest integer.
F6Fixed-point (6 decimals)1234.567000-1234.567000Zero-padded to 6 decimal places.
GGeneral1234.567-1234.567
N or N2Number1,234.57-1,234.57Includes a thousands separator based on the specified culture.
N0Number (no decimals)1,235-1,235Rounds to nearest integer.
P or P2Percent123,456.70%-123,456.70%
P0Percent (no decimals)123,457%-123,457%Rounds to nearest integer.
RRound-trip1234.567-1234.567Preserves precision.
XHexadecimal4D2ErrorNumbers A-F are shown in uppercase.
X8Hexadecimal (8 digits)000004D2ErrorNumbers A-F are shown in uppercase. Zero-padded to 8 digits.
xHexadecimal (lowercase)4d2ErrorNumbers A-F are shown in lowercase.

Use the custom format strings listed in this table to format numbers. The values in the Example columns are based on the en_US culture setting.

PatternDescriptionExample (1234.567)Example (0.567)
0Digit placeholderRequiredRequired
#Digit placeholderOptionalOptional
.Decimal point1234.5670.567
,Thousands separator1,234.5670.567
%Percentage123456.7%56.7%
Per mille1234567‰567‰
E0Scientific notation1.234567E35.67E-1
;Section separatorFormat;NegFormat;ZeroFormat
<text>Literal string1234.567 units0.567 units
\Escape character1234.567%0.567%

By using the ; character, you can specify separate formats for positive, negative, and zero values. For example, Format(1234.567, "N2;N0;N2") returns 1,234.57, and Format(-1234.567, "N2;(F2);N0") returns (1,234.57).

This table shows additional examples of custom numeric formatting strings.

Format StringInputOutputDescription
#,##0.001234.5671,234.57Standard number with 2 decimals
#,##01234.5671,235Rounded to whole number
000000123000123Zero-padded to 6 digits
###.###123.456123.456Optional digits
#.##;(#.##);Zero-123.45(123.45)Negative format section
#.##;(#.##);Zero0ZeroZero format section
# 'units'123123 unitsLiteral text
#.##%0.123412.34%Percentage
#.##‰0.1234123.4‰Per mille

When the type parameter is omitted or is set to string, these rules apply:

  • If the formatString parameter is empty, the string is returned as-is.
  • If any format string value is provided, the value is treated as a string template placeholder.
  • If formatting fails, an exception is thrown.

These rules are applied when formatting strings:

  1. All format patterns are case-sensitive.
  2. Literal text must be enclosed in single or double quotes.
  3. Use a backslash (\) to escape special characters.
  4. If a pattern isn’t valid, an exception is thrown.
  5. Date patterns work only with date types, and numeric patterns workonly with numbers.
  6. Some patterns, such as day and month names, vary by culture.
  7. Numbers can have up to 15 significant digits.
  8. Use semicolon (;) to separate positive, negative, and zero formats.

Use the culture parameter to specify the culture to use for formatting. These locale codes are supported:

  • en-US (English - United States)
  • en-GB (English - United Kingdom)
  • fr-FR (French - France)
  • de-DE (German - Germany)
  • es-ES (Spanish - Spain)
  • ja-JP (Japanese - Japan)
  • zh-CN (Chinese - China)
  • zh-TW (Chinese - Taiwan)

The culture value determines the date format conventions, currency symbols, decimal separators, thousands separators, day names, and month names that are used for formatting.

The function returns the formatted value as a string. If formatting fails, it returns the original value as a string (backward compatibility). All format strings are case-sensitive.