# 时间段
Moment.js also has duration objects. Where a moment is defined as single points in time, durations are defined as a length of time.
Durations do not have a defined beginning and end date. They are contextless.
A duration is conceptually more similar to '2 hours' than to 'between 2 and 4 pm today'. As such, they are not a good solution to converting between units that depend on context.
For example, a year can be defined as 366 days, 365 days, 365.25 days, 12 months, or 52 weeks. Trying to convert years to days makes no sense without context. It is much better to use moment#diff for calculating days or years between two moments than to use Durations.
# 创建 1.6.0+
moment.duration(Number, String);
moment.duration(Number);
moment.duration(Object);
moment.duration(String);
2
3
4
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');
2
3
4
5
6
7
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
});
2
3
4
5
6
7
8
9
- 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
2
3
4
- As of 2.3.0, moment also supports parsing ISO 8601 durations.
moment.duration('P1Y2M3DT4H5M6S');
moment.duration('P1M');
2
# 拟人化 1.6.0+
moment.duration().humanize();
Sometimes, you want all the goodness of moment#from but you don't want to have to create two moments, you just want to display a length of time.
Entermoment.duration().humanize().
moment.duration(1, "minutes").humanize(); // a minute
moment.duration(2, "minutes").humanize(); // 2 minutes
moment.duration(24, "hours").humanize(); // a day
2
3
By default, the return string is suffixless. If you want a suffix, pass in true as seen below.
moment.duration(1, "minutes").humanize(true); // in a minute
For suffixes before now, pass in a negative number.
moment.duration(-1, "minutes").humanize(true); // a minute ago
# 毫秒 1.6.0+
moment.duration().milliseconds();
moment.duration().asMilliseconds();
2
To get the number of milliseconds in a duration, use
moment.duration().milliseconds(),It will return a number between 0 and 999.
moment.duration(500).milliseconds(); // 500
moment.duration(1500).milliseconds(); // 500
moment.duration(15000).milliseconds(); // 0
2
3
If you want the length of the duration in milliseconds, use moment.duration().asMilliseconds() instead.
moment.duration(500).asMilliseconds(); // 500
moment.duration(1500).asMilliseconds(); // 1500
moment.duration(15000).asMilliseconds(); // 15000
2
3
# 秒 1.6.0+
moment.duration().seconds();
moment.duration().asSeconds();
2
To get the number of seconds in a duration, use moment.duration().seconds().
It will return a number between 0 and 59.
moment.duration(500).seconds(); // 0
moment.duration(1500).seconds(); // 1
moment.duration(15000).seconds(); // 15
2
3
If you want the length of the duration in seconds, use moment.duration().asSeconds() instead.
moment.duration(500).asSeconds(); // 0.5
moment.duration(1500).asSeconds(); // 1.5
moment.duration(15000).asSeconds(); // 15
2
3
# 分钟 1.6.0+
moment.duration().minutes();
moment.duration().asMinutes();
2
As with the other getters for durations, moment.duration().minutes() gets the minutes (0 - 59).
moment.duration().asMinutes() gets the length of the duration in minutes.
# 小时 1.6.0+
moment.duration().hours();
moment.duration().asHours();
2
As with the other getters for durations, moment.duration().hours() gets the hours (0 - 23).
moment.duration().asHours() gets the length of the duration in hours.
# 天 1.6.0+
moment.duration().days();
moment.duration().asDays();
2
As with the other getters for durations, moment.duration().days() gets the days (0 - 29).
moment.duration().asDays() gets the length of the duration in days.
# 月 1.6.0+
moment.duration().months();
moment.duration().asMonths();
2
As with the other getters for durations, moment.duration().months() gets the months (0 - 11).
moment.duration().asMonths() gets the length of the duration in months.
Note
The length of a duration in months is defined as 30 days.
# 年 1.6.0+
moment.duration().years();
moment.duration().asYears();
2
As with the other getters for durations, moment.duration().years() gets the years.
moment.duration().asYears() gets the length of the duration in years.
Note
The length of a duration in years is defined as 365 days.
# 加法 2.1.0+
moment.duration().add(Number, String);
moment.duration().add(Number);
moment.duration().add(Duration);
moment.duration().add(Object);
2
3
4
Mutates the original duration by adding time.
The same keys and shorthands used to create durations can be used here as the second argument.
var a = moment.duration(1, 'd');
var b = moment.duration(2, 'd');
a.add(b).days(); // 3
2
3
# 减法 2.1.0+
moment.duration().subtract(Number, String);
moment.duration().subtract(Number);
moment.duration().subtract(Duration);
moment.duration().subtract(Object);
2
3
4
Mutates the original duration by subtracting time.
The same keys and shorthands used to create durations can be used here as the second argument.
var a = moment.duration(3, 'd');
var b = moment.duration(2, 'd');
a.subtract(b).days(); // 1
2
3
# 转换单位 2.1.0+
moment.duration().as(String);
As an alternate to Duration#asX, you can use Duration#as('x'). All the shorthand keys from moment#add apply here as well.
duration.as('hours');
duration.as('minutes');
duration.as('seconds');
duration.as('milliseconds');
2
3
4
# 取值 2.1.0+
moment.duration().get(String);
As an alternate to Duration#x() getters, you can use Duration#get('x'). All the shorthand keys from moment#add apply here as well.
duration.get('hours');
duration.get('minutes');
duration.get('seconds');
duration.get('milliseconds');
2
3
4
# 输出 JSON 2.9.0+
moment.duration().toJSON();
When serializing a duration object to JSON, it will be be represented as an ISO8601 string.
JSON.stringify({
postDuration : moment.duration(5, 'm')
}); // '{"postDuration":"PT5M"}'
2
3
# Is a Duration 1.6.0+
moment.isDuration(obj);
To check if a variable is a moment duration object, use moment.isDuration().
moment.isDuration() // false
moment.isDuration(new Date()) // false
moment.isDuration(moment()) // false
moment.isDuration(moment.duration()) // true
moment.isDuration(moment.duration(2, 'minutes')) // true
2
3
4
5