简体   繁体   English

如何在不到小时的时间内获取格式“00:34”moment.js

[英]How to get format "00:34" for duration less than hour moment.js

I have duration like:我有这样的持续时间:

{
_isValid: true
_milliseconds: 2040000
_days: 0
_months: 0
_data: {milliseconds: 0, seconds: 0, minutes: 31, hours: 3, days: 0, …}
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, …}
__proto__: Object
}

if I make duration.format("HH:mm") I get "34", how can I get "00:34"?如果我使 duration.format("HH:mm") 我得到“34”,我怎样才能得到“00:34”?

My code where I use this:我使用它的代码:

duration: timeEntries
            .slice(1)
            .reduce((prev, cur) => {
              return prev.add(cur.duration);
            }, moment.duration(timeEntries[0].duration))
            .format("HH:mm")

and my duration is 34, instead of 00:34, because duration is less than hour, in other cases, where time is bigger, it return a 1:30 for example我的持续时间是 34,而不是 00:34,因为持续时间小于小时,在其他情况下,如果时间更大,它返回 1:30 例如

Try this尝试这个

Duration object has _data which contains the time object we need for formatting. Duration 对象具有_data ,其中包含我们需要格式化的时间对象。

 //moment(moment.duration(duration_value)._data).format("HH:mm"); console.log(moment(moment.duration(3500000)._data).format("HH:mm"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM