Skip to content

Latest commit

 

History

History
174 lines (155 loc) · 5.53 KB

File metadata and controls

174 lines (155 loc) · 5.53 KB

Use Unicode Locale Data Markup Language (LDML) patterns to specify a custom format string. An LDML pattern consists of wildcard characters and characters displayed as is. The format property supports the following wildcard characters:

Numeric Formats

Format character Description
0 A digit. Displays '0' if the formatted number does not have a digit in that position.
# Any number of leading digits, a single digit, or nothing. If this character goes first in the format string, it can match multiple leading digits (before the decimal point). Subsequent characters match a single digit. If the formatted number does not have a digit in the corresponding position, it displays nothing.
For example, if you apply format "#0.#" to "123.45", the result is "123.5".
. A decimal separator.
Actual character depends on locale.
, A group separator.
Actual character depends on locale.
% The percent sign. Multiplies the input value by 100.
; Separates positive and negative format patterns.
For example, the "#0.##;(#0.##)" format displays a positive number according to the pattern before the semicolon (";"), and a negative number according to the pattern after the semicolon (";").
If you do not use this character and the additional pattern, negative numbers display a minus ("-") prefix.
Escape characters You can display the special characters above as literals if you enclose them in single quotation marks.
For example, '%'.
Other characters You can add any literal characters to the beginning or end of the format string.

The examples below demonstrate the behavior of "#" and "0" in fractional numbers:

<!-- tab: JavaScript -->
const number = 1234.567;

// Leave the first digit before the decimal point and round up the decimal
format: "0.0" // 4.6

const smallNumber = 0.1234;

// Display nothing in place of a digit
format: "#.#" // .1

const largeNumber = 123456.789;

// Add a group separator
format: ",##0.###" // 123,456.789

The examples below show different ways to apply percentage formatting to decimals. Use caution if your format string starts with a zero ('0'), because the formatted number may lose leading digits.

<!-- tab: JavaScript -->
const smallNumber = 0.01234;

// Represent as a percentage and limit to two decimal digits
format: "#0.##%" // 1.23%

// Add a percent sign and limit to two decimal digits
format: "#0.##'%'" // 0.01%

Date-Time Formats

Format character Description
y A calendar year.
Q A quarter number or name.
Available combinations with example: "Q" - "2", "QQ" - "02", "QQQ" - "Q2" and "QQQQ" - "2nd quarter".
M A month number or name.
Available combinations with example: "M" - "9", "MM" - "09", "MMM" - "Sep", "MMMM" - "September", "MMMMM" - "S".
d A month day.
E A week day name.
Available combinations with example: "E", "EE" or "EEE" - "Tue", "EEEE" - "Tuesday", "EEEEE" - "T".
a A day period (am or pm).
h An hour. From 1 to 12.
H An hour. From 0 to 23.
m A minute.
s A second.
S A fractional second.
'' (two single quotes) Literal text. Text enclosed in two single quotes is shown as-is.
<!-- tab: JavaScript -->
const date = new Date(2021, 6, 15, 20, 45, 34);

format: "MM/dd/yyyy" // 07/15/2021
format: "MM/dd/yy" // 07/15/21
format: "dd.MM.yyyy" // 15.07.2021
format: "MMMM dd, yyyy" // July 15, 2021
format: "EEEE, MMMM dd" // Thursday, July 15
format: "HH:mm:ss" // 20:45:34
format: "hh:mm a" // 08:45 PM
format: "MMMM dd, yyyy HH:mm:ss" // July 15, 2021 20:45:34

[note] Reference the Globalize library in your application to use other numeric or datetime format characters.

#####See Also#####