String + Format1.0.0+

  1. moment(String, String);
  2. moment(String, String, String);
  3. moment(String, String, Boolean);
  4. moment(String, String, String, Boolean);

If you know the format of an input string, you can use that to parse a moment.

  1. moment("12-25-1995", "MM-DD-YYYY");

The parser ignores non-alphanumeric characters, so both of the following will return the same thing.

  1. moment("12-25-1995", "MM-DD-YYYY");
  2. moment("12/25/1995", "MM-DD-YYYY");

The parsing tokens are similar to the formatting tokens used in moment#format.

Year, month, and day tokens

Tokens are case-sensitive.

InputExampleDescription
YYYY20144 or 2 digit year
YY142 digit year
Y-25Year with any number of digits and sign
Q1..4Quarter of year. Sets month to first month in quarter.
M MM1..12Month number
MMM MMMMJan..DecemberMonth name in locale set by moment.locale()
D DD1..31Day of month
Do1st..31stDay of month with ordinal
DDD DDDD1..365Day of year
X1410715640.579Unix timestamp
x1410715640579Unix ms timestamp

YYYY from version 2.10.5 supports 2 digit years, and converts them to a yearnear 2000 (same as YY).

Y was added in 2.11.1. It will match any number, signed or unsigned. It is useful for years that are not 4 digits or are before the common era. It can be used for any year.

Week year, week, and weekday tokens

For these, the lowercase tokens use the locale aware week start days, and the uppercase tokens use the ISO week date start days.

Tokens are case-sensitive.

InputExampleDescription
gggg2014Locale 4 digit week year
gg14Locale 2 digit week year
w ww1..53Locale week of year
e0..6Locale day of week
ddd ddddMon…SundayDay name in locale set by moment.locale()
GGGG2014ISO 4 digit week year
GG14ISO 2 digit week year
W WW1..53ISO week of year
E1..7ISO day of week

Locale aware formats

Locale aware date and time formats are also available using LT LTS L LL LLL LLLL. They were added in version 2.2.1, except LTS which was added2.8.4.

Tokens are case-sensitive.

InputExampleDescription
L04/09/1986Date (in local format)
LLSeptember 4 1986Month name, day of month, year
LLLSeptember 4 1986 8:30 PMMonth name, day of month, year, time
LLLLThursday, September 4 1986 8:30 PMDay of week, month name, day of month, year, time
LT08:30 PMTime (without seconds)
LTS08:30:00 PMTime (with seconds)

Hour, minute, second, millisecond, and offset tokens

Tokens are case-sensitive.

InputExampleDescription
H HH0..23Hours (24 hour time)
h hh1..12Hours (12 hour time used with a A.)
k kk1..24Hours (24 hour time from 1 to 24)
a Aam pmPost or ante meridiem (Note the one character a p are also considered valid)
m mm0..59Minutes
s ss0..59Seconds
S SS SSS0..999Fractional seconds
Z ZZ+12:00Offset from UTC as +-HH:mm, +-HHmm, or Z

From version 2.10.5: fractional second tokens length 4 up to 9 can parseany number of digits, but will only consider the top 3 (milliseconds). Use ifyou have the time printed with many fractional digits and want to consume theinput.

Note that the number of S characters provided is only relevant when parsing in strict mode.In standard mode, S, SS, SSS, SSSS are all equivalent, and interpreted as fractions of a second.For example, .12 is always 120 milliseconds, passing SS will not cause it to be interpreted as 12 milliseconds.

Z ZZ were added in version 1.2.0.

S SS SSS were added in version 1.6.0.

X was added in version 2.0.0.

k kk was added in version 2.18.0

Unless you specify a time zone offset, parsing a string will create a date in the current time zone.

  1. moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time
  2. moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC

If the moment that results from the parsed input does not exist, moment#isValid will return false.

  1. moment("2010 13", "YYYY MM").isValid(); // false (not a real month)
  2. moment("2010 11 31", "YYYY MM DD").isValid(); // false (not a real day)
  3. moment("2010 2 29", "YYYY MM DD").isValid(); // false (not a leap year)
  4. moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)

As of version 2.0.0, a locale key can be passed as the third parameter to moment() and moment.utc().

  1. moment('2012 juillet', 'YYYY MMM', 'fr');
  2. moment('2012 July', 'YYYY MMM', 'en');

Moment's parser is very forgiving, and this can lead to undesired/unexpected behavior.

For example, the following behavior can be observed:

  1. moment('2016 is a date', 'YYYY-MM-DD').isValid() //true, 2016 was matched

Previous to 2.13.0 the parser exhibited the following behavior. This has been corrected.

  1. moment('I am spartacus', 'h:hh A').isValid(); //true - the 'am' matches the 'A' flag.

As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly, including delimeters.

  1. moment('It is 2012-05-25', 'YYYY-MM-DD').isValid(); // true
  2. moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid(); // false
  3. moment('2012-05-25', 'YYYY-MM-DD', true).isValid(); // true
  4. moment('2012.05.25', 'YYYY-MM-DD', true).isValid(); // false

You can use both locale and strictness.

  1. moment('2012-10-14', 'YYYY-MM-DD', 'fr', true);

Strict parsing is frequently the best parsing option. For more information about choosing strict vs forgiving parsing, see the parsing guide.

Parsing two digit years

By default, two digit years above 68 are assumed to be in the 1900's and years 68 or below are assumed to be in the 2000's. This can be changed by replacing the moment.parseTwoDigitYear method. The only argument of this method is a string containing the two years input by the user, and should return the year as an integer.

  1. moment.parseTwoDigitYear = function(yearString) {
  2. return parseInt(yearString) + 2000;
  3. }

Parsing glued hour and minutes

From version 2.11.0 parsing hmm, Hmm, hmmss and Hmmss is supported:

  1. moment("123", "hmm").format("HH:mm") === "01:23"
  2. moment("1234", "hmm").format("HH:mm") === "12:34"