MomentJS - 時長



MomentJS 提供了一個重要的功能,稱為時長 (durations),用於處理給定單位的時間長度。在本節中,您將詳細瞭解這一點。

時長可用方法

下表顯示了時長中可用於不同單位的各種方法:

方法 語法
建立

moment.duration(數字, 字串);

moment.duration(數字);

moment.duration(物件);

moment.duration(字串);

克隆

moment.duration().clone();

人性化

moment.duration().humanize();

毫秒

moment.duration().milliseconds();

moment.duration().asMilliseconds();

moment.duration().seconds();

moment.duration().asSeconds();

分鐘

moment.duration().minutes();

moment.duration().asMinutes();

小時

moment.duration().hours();

moment.duration().asHours();

moment.duration().days();

moment.duration().asDays();

moment.duration().weeks();

moment.duration().asWeeks();

moment.duration().months();

moment.duration().asMonths();

moment.duration().years();

moment.duration().asYears();

新增時間

moment.duration().add(數字, 字串);

moment.duration().add(數字);

moment.duration().add(時長);

moment.duration().add(物件);

減去時間

moment.duration().subtract(數字, 字串);

moment.duration().subtract(數字);

moment.duration().subtract(時長);

moment.duration().subtract(物件);

將時長與 diff 一起使用

var duration = moment.duration(x.diff(y))

作為時間單位

moment.duration().as(字串);

獲取時間單位

duration.get('hours');

duration.get('minutes');

duration.get('seconds');

duration.get('milliseconds');

作為 JSON

moment.duration().toJSON();

是否是時長

moment.isDuration(物件);

作為 ISO 8601 字串

moment.duration().toISOString();

語言環境

moment.duration().locale();

moment.duration().locale(字串);

廣告