Utilities

Moment exposes some methods which may be useful to people extending the library or writing custom parsers.

Normalize Units2.3.0+

  1. moment.normalizeUnits(String);

Many of Moment's functions allow the caller to pass in aliases for unit enums. For example, all of the gets below are equivalent.

  1. var m = moment();
  2. m.get('y');
  3. m.get('year');
  4. 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.

  1. moment.normalizeUnits('y'); // 'year'
  2. moment.normalizeUnits('Y'); // 'year'
  3. moment.normalizeUnits('year'); // 'year'
  4. moment.normalizeUnits('years'); // 'year'
  5. moment.normalizeUnits('YeARS'); // 'year'

Invalid2.3.0+

  1. moment.invalid(Object);

You can create your own invalid Moment objects, which is useful in making your own parser.

  1. var m = moment.invalid();
  2. m.isValid(); // false
  3. m.format(); // 'Invalid date'
  4. 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.

  1. var m = moment.invalid({invalidMonth: 'Actober'});
  2. 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().