Date, Time and Number Formatting

The following sections are cited from https://momentjs.com/docs/#/displaying/ for Date and Time formatting and http://www.gwtproject.org/ for Number formatting. 

Formatting Date and Time

Patterns


Token

Output

Month



M1 2 ... 11 12
Mo1st 2nd ... 11th 12th
MM01 02 ... 11 12
MMMJan Feb ... Nov Dec
MMMMJanuary February ... November December
QuarterQ1 2 3 4
Qo1st 2nd 3rd 4th
Day of Month

D1 2 ... 30 31
Do1st 2nd ... 30th 31st
DD01 02 ... 30 31
Day of Year

DDD1 2 ... 364 365
DDDo1st 2nd ... 364th 365th
DDDD001 002 ... 364 365
Day of Week



d0 1 ... 5 6
do0th 1st ... 5th 6th
ddSu Mo ... Fr Sa
dddSun Mon ... Fri Sat
ddddSunday Monday ... Friday Saturday
Day of Week (Locale)e0 1 ... 5 6
Day of Week (ISO)E1 2 ... 6 7
Week of Year

w1 2 ... 52 53
wo1st 2nd ... 52nd 53rd
ww01 02 ... 52 53
Week of Year (ISO)

W1 2 ... 52 53
Wo1st 2nd ... 52nd 53rd
WW01 02 ... 52 53
Year

YY70 71 ... 29 30
YYYY1970 1971 ... 2029 2030
Y1970 1971 ... 9999 +10000 +10001
Note: This complies with the ISO 8601 standard for dates past the year 9999
Week Yeargg70 71 ... 29 30
gggg1970 1971 ... 2029 2030
Week Year (ISO)GG70 71 ... 29 30
GGGG1970 1971 ... 2029 2030
AM/PMAAM PM
aam pm
Hour




H0 1 ... 22 23
HH00 01 ... 22 23
h1 2 ... 11 12
hh01 02 ... 11 12
k1 2 ... 23 24
kk01 02 ... 23 24
Minutem0 1 ... 58 59
mm00 01 ... 58 59
Seconds0 1 ... 58 59
ss00 01 ... 58 59
Fractional Second


S0 1 ... 8 9
SS00 01 ... 98 99
SSS000 001 ... 998 999
SSSS ... SSSSSSSSS000[0..] 001[0..] ... 998[0..] 999[0..]
Time Zone

z or zzEST CST ... MST PST
Note: as of 1.6.0, the z/zz format tokens have been deprecated from plain moment objects. Read more about it here. However, they *do* work if you are using a specific time zone with the moment-timezone addon.
Z-07:00 -06:00 ... +06:00 +07:00
ZZ-0700 -0600 ... +0600 +0700
Unix TimestampX1360013296
Unix Millisecond Timestampx1360013296123

Localized Formats

Because preferred formatting differs based on locale, there are a few tokens that can be used to format a moment based on its locale.

There are upper and lower case variations on the same formats. The lowercase version is intended to be the shortened version of its uppercase counterpart.

TimeLT8:30 PM
Time with secondsLTS8:30:25 PM
Month numeral, day of month, yearL09/04/1986
l9/4/1986
Month name, day of month, yearLLSeptember 4, 1986
llSep 4, 1986
Month name, day of month, year, timeLLLSeptember 4, 1986 8:30 PM
lllSep 4, 1986 8:30 PM
Month name, day of month, day of week, year, timeLLLLThursday, September 4, 1986 8:30 PM
llllThu, Sep 4, 1986 8:30 PM

Escaping Characters

To escape characters in format strings, you can wrap the characters in square brackets.

moment().format('[today] dddd'); // 'today Sunday'

Formatting Numbers

Patterns

Formatting is based on customizable patterns that can include a combination of literal characters and special characters that act as placeholders and are replaced by their localized counterparts. Many characters in a pattern are taken literally; they are unchanged during formatting. Special characters, on the other hand, stand for other characters, strings, or classes of characters. For example, the '#' character is replaced by a localized digit.

Often the replacement character is the same as the pattern character. In the U.S. locale, for example, the ',' grouping character is replaced by the same character ','. However, the replacement is still actually happening, and in a different locale, the grouping character may change to a different character, such as '.'. Some special characters affect the behavior of the formatter by their presence. For example, if the percent character is seen, then the value is multiplied by 100 before being displayed.

The characters listed below are used in patterns. Localized symbols use the corresponding characters taken from corresponding locale symbol collection. To insert a special character in a pattern as a literal (that is, without any special meaning) the character must be quoted. There are some exceptions to this which are noted below.


Symbol LocationLocalized?Meaning
0NumberYes

Digit

There is support for K, M, B abbreviations in custom formatting: 0.0K / 0.0M / 0.0B where "0" drives the number of digits (# can be used too). 

Example for number "44,774.13":

  • Formatted as "0K" returns "45K"
  • Formatted as "0.0K" returns "44.8K"
  • Formatted as "0.00K" returns "44.77K"
 # NumberYes

Digit, zero shows as absent

Examples of use: 

  • # represents one digit but it is optional, â€‹so if you format 10.0001 as #'###.## you will end up with 10
  • If you want to force that a certain digit shows up, you use 0 instead of #
    • ​10.001 formatted with 0'000.00 would be displayed as 0'010.00 (just an example)
    • ​more typical pattern would be #'###.00 -> 10.00
      or
    • #'###.000 -> 10.001
  • If you format 1 with #'###.00 you will end up with 1.00
  • Also, most characters are not interpreted, so if the format string is "Value: #.## USD" and the number is 1.2, the formatting displays "Value: 1.2 USD"
NumberYesDecimal separator or monetary decimal separator
NumberYesMinus sign
NumberYesGrouping separator
NumberYesSeparates mantissa and exponent in scientific notation; need not be quoted in prefix or suffix
Subpattern boundaryYesSeparates positive and negative subpatterns  (see details below the table)
Prefix or Suffix YesMultiply by 100 and show as percentage
? (\u2030) Prefix or Suffix YesMultiply by 1000 and show as per million
¤ (\u00A4) Prefix or Suffix NoCurrency sign, replaced by currency symbol; if doubled, replaced by international currency symbol; if present in a pattern, the monetary decimal separator is used instead of the decimal separator
Prefix or Suffix NoUsed to quote special characters in a prefix or suffix; for example, "'#'#" formats 123 to "#123"; to create a single quote itself, use two in succession, such as "# o''clock"

If there is a semicolon in the pattern, then the first part (subpattern) is for positive values and the second is for negative ones, e.g. "#,##0.00;(#,##0.00)". Each subpattern has a prefix, a numeric part, and a suffix. If there is no explicit negative subpattern, the negative subpattern is the localized minus sign prefixed to the positive subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00". If there is an explicit negative subpattern, it serves only to specify the negative prefix and suffix; the number of digits, minimal digits, and other characteristics are ignored in the negative subpattern. That means that "#,##0.0#;(#)" has precisely the same result as "#,##0.0#;(#,##0.0#)".

The prefixes, suffixes, and various symbols used for infinity, digits, thousands separators, decimal separators, etc. may be set to arbitrary values, and they will appear properly during formatting. However, care must be taken that the symbols and strings do not conflict, or parsing will be unreliable. For example, the decimal separator and thousands separator should be distinct characters, or parsing will be impossible.

The grouping separator is a character that separates clusters of integer digits to make large numbers more legible. It commonly used for thousands, but in some locales it separates ten-thousands. The grouping size is the number of digits between the grouping separators, such as 3 for "100,000,000" or 4 for "1 0000 0000".

Pattern Grammar (BNF)

The pattern itself uses the following grammar:

pattern:=subpattern (';' subpattern)?
subpattern:=prefix? number exponent? suffix?
number:=(integer ('.' fraction)?) | sigDigits
prefix:='\u0000'..'\uFFFD' - specialCharacters
suffix:='\u0000'..'\uFFFD' - specialCharacters
integer:='#'* '0'*'0'
fraction:='0'* '#'*
sigDigits:='#'* '@''@'* '#'*
exponent:='E' '+'? '0'* '0'
padSpec:='*' padChar
padChar:='\u0000'..'\uFFFD' - quote


Notation:

X* 0 or more X symbols
X? 0 or 1 X symbols
X|Y either X or Y
C..Fany symbol from C to F included
S-T symbols in S, excluding those in T

The first subpattern is for positive numbers. The second (optional) subpattern is for negative numbers.

Found an issue in documentation? Write to us.

 
Pricefx version 13.1