# util

back

# 工具类函数

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

Moment公开了一些方法,这些方法可能对扩展库或编写自定义解析器的人有用。

标准化单位 2.3.0+

moment.normalizeUnits(String);
1

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

Moment的许多功能都允许调用者传递单位枚举的别名。例如,下面的所有获取都是等效的。

var m = moment();
m.get('y');
m.get('year');
m.get('years');
1
2
3
4

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'
1
2
3
4
5

无效对象 2.3.0+

moment.invalid(Object);
1

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
1
2
3
4

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'
1
2

You need not specify parsing flags recognized by Moment; the Moment will be invalid nonetheless, and the parsing flags will be returned by parsingFlags().