Moment.js中文文档系列之八时间段(Durations)
- 创建时间段:
duration()
- 人性化:
humanize()
- 毫秒:
milliseconds()
- 秒:
seconds()
- 分:
minutes()
- 小时:
hours()
- 天:
days()
- 月:
months()
- 年:
years()
- 增加时间:
add()
- 减少时间:
subtract()
- 单位转换:
as()
- 取值:
get()
- 转换为JSON:
toJSON()
- 是否时间段:
isDuration()
Moment.js
中也有时间段对象。一个时间moment
被定义为一个单点时刻,而时间段被定义为一个时间长度。
时间段没有定义开始和结束时间,它们是一种前后的关系。一个时间段更类似于“2小时”,而不是“今天下午的2点到4点之间”。
例如,一年可以被定义为366天、365天、365.25天、12个月或52周。计算两个时间之间的天或年时,使用moment#diff 比使用Durations
更好。
1. 创建时间段:duration()
moment.duration(Number, String); moment.duration(Number); moment.duration(Object); moment.duration(String);
创建一个时间段,通过moment.duration()
方法并传入一个毫秒级的时间戳。
moment.duration(100); // 100 毫秒
如果你想在创建时间段时使用非毫秒级的单位,你可以像这样传入时间单位:
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#add
和moment#subtract
方法中使用简写形式在这儿也同样适用。
Key | Shorthand |
---|---|
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 });
格式是一个以冒号分隔的时、分、秒的字符串,如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'); //added in 2.3.0
2. 人性化:humanize()
moment.duration().humanize();
有时,你想像moment#from一样友好显示,但不想创建两个时间。
moment.duration(1, "minutes").humanize(); // a minute moment.duration(2, "minutes").humanize(); // 2 minutes moment.duration(24, "hours").humanize(); // a day
默认,其返回值是无后缀的。如果需要带后,可以传一个参数。
moment.duration(1, "minutes").humanize(true); // in a minute
如果是相对当前时间的前缀,可以传入一个负数。
moment.duration(-1, "minutes").humanize(true); // a minute ago
3. 毫秒:milliseconds()
moment.duration().milliseconds(); moment.duration().asMilliseconds();
获取一个数字表示的毫秒数,使用moment.duration().milliseconds()
,其返回是一个0〜999之间的数字。
moment.duration(500).milliseconds(); // 500 moment.duration(1500).milliseconds(); // 500 moment.duration(15000).milliseconds(); // 0
如果要获取的是一个毫秒级的时间段长度,使用moment.duration().asMilliseconds()
方法代替。
moment.duration(500).asMilliseconds(); // 500 moment.duration(1500).asMilliseconds(); // 1500 moment.duration(15000).asMilliseconds(); // 15000
4. 秒:seconds()
moment.duration().seconds(); moment.duration().asSeconds();
获取一个数字表示的秒数,使用moment.duration().seconds()
,其返回是一个0〜59之间的数字。
moment.duration(500).seconds(); // 0 moment.duration(1500).seconds(); // 1 moment.duration(15000).seconds(); // 15
如果要获取的是一个秒级的时间段长度,使用moment.duration().asSeconds()
方法代替。
moment.duration(500).asSeconds(); // 0.5 moment.duration(1500).asSeconds(); // 1.5 moment.duration(15000).asSeconds(); // 15
5. 分:minutes()
moment.duration().minutes(); moment.duration().asMinutes();
获取其它的时间段,moment.duration().minutes()
可以获取分钟数(0〜59),moment.duration().asMinutes()
可以获取表示分的长度。
6. 小时:hours()
moment.duration().hours(); moment.duration().asHours();
获取其它的时间段,moment.duration().hours()
可以获取小时数(0〜23),moment.duration().asHours()
可以获取表示小时的长度。
7. 天:days()
moment.duration().days(); moment.duration().asDays();
获取其它的时间段,moment.duration().days()
可以获取天数(0〜23),moment.duration().asHours()
可以获取表示天的长度。
8. 月:months()
moment.duration().months(); moment.duration().asMonths();
获取其它的时间段,moment.duration().months()
可以获取月数(0〜11),moment.duration().asMonths()
可以获取表示月的长度。
注意:一月的时间长度被定义为30天。
9. 年:years()
moment.duration().years(); moment.duration().asYears();
获取其它的时间段,moment.duration().years()
可以获取年份,moment.duration().asYears()
可以获取表示年的长度。
注意:一年的时间长度被定义为365天。
10. 增加时间:add()
moment.duration().add(Number, String); moment.duration().add(Number); moment.duration().add(Duration); moment.duration().add(Object);
对原时间段增加时间。
在创建时间段时使用的简写形式的时间单位,在这里也同样适用。可以第二个参数中传入。
var a = moment.duration(1, 'd'); var b = moment.duration(2, 'd'); a.add(b).days(); // 3
11. 减少时间:subtract()
moment.duration().subtract(Number, String); moment.duration().subtract(Number); moment.duration().subtract(Duration); moment.duration().subtract(Object);
对原时间段减少时间。
在创建时间段时使用的简写形式的时间单位,在这里也同样适用。可以第二个参数中传入。
var a = moment.duration(3, 'd'); var b = moment.duration(2, 'd'); a.subtract(b).days(); // 1
12. 单位转换:as()
moment.duration().as(String);
替换Duration#asX
等方法,可以使用Duration#as('x')
,moment#add中使用的单位简写,在这里也同样适用。
duration.as('hours'); duration.as('minutes'); duration.as('seconds'); duration.as('milliseconds');
13. 取值:get()
moment.duration().get(String);
替换Duration#x
等方法,可以使用Duration#get('x')
,moment#add中使用的单位简写,在这里也同样适用。
duration.get('hours'); duration.get('minutes'); duration.get('seconds'); duration.get('milliseconds');
14. 转换为JSON:toJSON()
moment.duration().toJSON();
将时间段对象序列化成JSON时,其会按ISO8601标准的字符串进行转换。
JSON.stringify({ postDuration : moment.duration(5, 'm') }); // '{"postDuration":"PT5M"}'
15. 是否时间段:isDuration()
moment.isDuration(obj);
检查是否是一个有效的时间段对象,使用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
转自 https://itbilu.com/nodejs/npm/4JkB42p-x.html
总结 :
moment.duration(now()) 获取当前时间
moment.duration(now()).asHours() 获取当前时间的小时时间段
asMilliseconds()毫秒
asSeconds()秒
asMinutes()分
asHours()时
asDays()天
asMonths()月
asYears()年
不加 as 的时候,是获取时间值