duration()


moment.duration(Number, String);
moment.duration(Number);
moment.duration(Object);
moment.duration(String);

要创建时长,则调用 moment.duration(),并以毫秒为单位。

moment.duration(100); // 100 毫秒

如果要使用毫秒以外的其他度量单位来创建 moment,则也可以传入度量单位。

moment.duration(2, 'seconds');
moment.duration(2, 'minutes');
moment.duration(2, 'hours');
moment.duration(2, 'days');
moment.duration(2, 'weeks');
moment.duration(2, 'months');
moment.duration(2, 'years');

同样,moment#addmoment#subtract的简写在这里也适用。

简写
years y
months M
weeks w
days d
hours h
minutes m
seconds s
milliseconds ms

moment#add 相似,如果需要多个不同的度量单位,则可以传入值的对象。

moment.duration({
    seconds: 2,
    minutes: 2,
    hours: 2,
    days: 2,
    weeks: 2,
    months: 2,
    years: 2
});

2.1.0 开始,moment 支持解析 ASP.NET 风格的时间跨度。 支持以下格式。

格式是以冒号分隔的小时、分钟、秒钟的字符串,例如 23:59:59。 天数可以加上点分隔符,如 7.23:59:59。 还支持部分秒数如23:59:59.999`。

moment.duration('23:59:59');
moment.duration('23:59:59.999');
moment.duration('7.23:59:59.999');
moment.duration('23:59'); // 新增于 2.3.0

2.3.0 开始,moment 还支持解析 ISO 8601 时长。

moment.duration('P1Y2M3DT4H5M6S');
moment.duration('P1M');

2.11.0 开始,支持时长的格式字符串,其中天数和剩下的时间之间有空格。

moment.duration('7 23:59:59.999');

2.13.0 开始,解析时长时支持混合的负号和正号。

moment.duration('PT-6H3M')

2.18.0 开始,支持无效的时长,类似于无效的时刻。 要创建无效的时长,可以为单位的值传入 NaN

在即将发布的版本中,预期无效的时长可以覆盖更多情况(例如单位的空值)。

moment.duration(NaN);
moment.duration(NaN, 'days');
moment.duration.invalid();
moment.duration(Number, String);
moment.duration(Number);
moment.duration(Object);
moment.duration(String);

To create a duration, call moment.duration() with the length of time in milliseconds.

moment.duration(100); // 100 milliseconds

If you want to create a moment with a unit of measurement other than milliseconds, you can pass the unit of measurement as well.

moment.duration(2, 'seconds');
moment.duration(2, 'minutes');
moment.duration(2, 'hours');
moment.duration(2, 'days');
moment.duration(2, 'weeks');
moment.duration(2, 'months');
moment.duration(2, 'years');

The same shorthand for moment#add and moment#subtract works here as well.

Key Shorthand
years y
months M
weeks w
days d
hours h
minutes m
seconds s
milliseconds ms

Much like moment#add, you can pass an object of values if you need multiple different units of measurement.

moment.duration({
    seconds: 2,
    minutes: 2,
    hours: 2,
    days: 2,
    weeks: 2,
    months: 2,
    years: 2
});

As of 2.1.0, moment supports parsing ASP.NET style time spans. The following formats are supported.

The format is an hour, minute, second string separated by colons like 23:59:59. The number of days can be prefixed with a dot separator like so 7.23:59:59. Partial seconds are supported as well 23:59:59.999.

moment.duration('23:59:59');
moment.duration('23:59:59.999');
moment.duration('7.23:59:59.999');
moment.duration('23:59'); // added in 2.3.0

As of 2.3.0, moment also supports parsing ISO 8601 durations.

moment.duration('P1Y2M3DT4H5M6S');
moment.duration('P1M');

As of 2.11.0, duration format strings with a space between days and rest is supported.

moment.duration('7 23:59:59.999');

As of 2.13.0, mixed negative and positive signs are supported when parsing durations.

moment.duration('PT-6H3M')

As of 2.18.0, invalid durations are supported, similarly to invalid moments. To create an invalid duration you can pass NaN for a value of a unit.

In upcoming releases expect invalid durations to cover more cases (like null values for units).

moment.duration(NaN);
moment.duration(NaN, 'days');
moment.duration.invalid();