用来表示一段时间,而不是具体的日期时间点,时长没有定义的开始和结束日期。 它们是无上下文的。

通常用来实现计时功能(正计时、倒计时)

英文字典

别名键 简写键
year years y
quarter quarters Q
month months M
week weeks w
day days d
hour hours h
minute minutes m
second seconds s
millisecond milliseconds ms

定义一个时长

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

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

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

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.duration().clone();

增加时长

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

减少时长

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

获取总时长(as 方法)

moment.duration().as(String);
moment.duration().as(String);

moment.duration(1, 'hours').as('hours'); // 1
moment.duration(2, 'minutes').as('minutes'); // 2
moment.duration(3, 'seconds').as('seconds'); // 3
moment.duration(4, 'milliseconds').as('milliseconds'); // 4

获取总时长(毫秒)

moment.duration().asMilliseconds();

moment.duration(500).asMilliseconds(); // 500
moment.duration(1500).asMilliseconds(); // 1500
moment.duration(15000).asMilliseconds(); // 15000

获取总时长(秒)

moment.duration().asSeconds();

moment.duration(500).asSeconds(); // 0.5
moment.duration(1500).asSeconds(); // 1.5
moment.duration(15000).asSeconds(); // 15

获取总时长(分)

moment.duration().asMinutes();

获取总时长(小时)

moment.duration().asHours();

获取总时长(天数)

moment.duration().asDays();

获取总时长(第几个周)

moment.duration().asWeeks();

获取总时长(月)

moment.duration().asMonths();

获取总时长(年)

moment.duration().asYears();

获取时长的某一单位(get 方法)

moment.duration().get(String);
moment.duration().get(String);

moment.duration(25, 'hours').get('hours'); // 1
moment.duration(62, 'minutes').get('minutes'); // 2
moment.duration(63, 'seconds').get('seconds'); // 3
moment.duration(1004, 'milliseconds').get('milliseconds'); // 4

获取时长的某一单位(毫秒)

0 ~ 999

moment.duration().milliseconds();

moment.duration(500).milliseconds(); // 500
moment.duration(1500).milliseconds(); // 500
moment.duration(15000).milliseconds(); // 0

获取时长的某一单位(秒)

0 ~ 59

moment.duration().seconds();

moment.duration(500).seconds(); // 0
moment.duration(1500).seconds(); // 1
moment.duration(15000).seconds(); // 15

获取时长的某一单位(分)

0 ~ 59

moment.duration().minutes();

获取时长的某一单位(小时)

0 ~ 23

moment.duration().hours();

获取时长的某一单位(天数)

0 ~ 30

moment.duration().days();

获取时长的某一单位(第几个周)

0 ~ 4

moment.duration().weeks();

获取时长的某一单位(月)

0 ~ 4

moment.duration().months();

获取时长的某一单位(年)

0 ~ 4

moment.duration().years();