If you know the format of an input string, you can use that to parse a moment.
The parser ignores non-alphanumeric characters, so both of the following will return the same thing.
moment("12-25-1995", "MM-DD-YYYY");
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.
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 start days.
Tokens are case-sensitive.
Locale aware formats
Tokens are case-sensitive.
Hour, minute, second, millisecond, and offset tokens
Tokens are case-sensitive.
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
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time
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.
As of version 2.0.0, a locale key can be passed as the third parameter to moment()
and moment.utc()
.
moment('2012 juillet', 'YYYY MMM', 'fr');
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:
Previous to 2.13.0 the parser exhibited the following behavior. This has been corrected.
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.
You can use both locale and strictness.
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
moment.parseTwoDigitYear = function(yearString) {
return parseInt(yearString) + 2000;
}
Parsing glued hour and minutes
From version 2.11.0 parsing hmm
, Hmm
, hmmss
and Hmmss
is supported:
moment("123", "hmm").format("HH:mm") === "01:23"