Utilities
Moment exposes some methods which may be useful to people extending the library or writing custom parsers.
Normalize Units2.3.0+
moment.normalizeUnits(String);
Many of Moment's functions allow the caller to pass in aliases for unit enums. For example, all of the get
s below are equivalent.
var m = moment();
m.get('y');
m.get('year');
m.get('years');
If you're extending the library, you may want access to Moment's facilities for that in order to better align your functionality with Moment's.
moment.normalizeUnits('y'); // 'year'
moment.normalizeUnits('Y'); // 'year'
moment.normalizeUnits('year'); // 'year'
moment.normalizeUnits('years'); // 'year'
moment.normalizeUnits('YeARS'); // 'year'
Invalid2.3.0+
moment.invalid(Object);
You can create your own invalid Moment objects, which is useful in making your own parser.
var m = moment.invalid();
m.isValid(); // false
m.format(); // 'Invalid date'
m.parsingFlags().userInvalidated; // true
invalid
also accepts an object which specifies which parsing flags to set. This will not set the userInvalidated
parsing flag unless it's one of the properties specified.
var m = moment.invalid({invalidMonth: 'Actober'});
m.parsingFlags().invalidMonth; // 'Actober'
You need not specify parsing flags recognized by Moment; the Moment will be invalid nonetheless, and the parsing flags will be returned by parsingFlags()
.