最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display 0 minute as '00:00' in moment.js? - Stack Overflow

programmeradmin4浏览0评论

I would like to display 0 minute in 'HH:mm' format. When I use moment.duration(0, 'minutes').format('HH:mm') it returns '00' instead of '00:00'. Is there a format type in moment.js which displays 0 minute as '00:00'?

I would like to display 0 minute in 'HH:mm' format. When I use moment.duration(0, 'minutes').format('HH:mm') it returns '00' instead of '00:00'. Is there a format type in moment.js which displays 0 minute as '00:00'?

Share Improve this question asked Apr 10, 2018 at 11:36 bugovicsbbugovicsb 4562 gold badges7 silver badges16 bronze badges 2
  • 1 moment.duration(0, 'minutes').format('HH:mm') throws an error because format isn't a valid method of duration. How did you get that working? – 31piy Commented Apr 10, 2018 at 11:40
  • include moment-duration-format.js (github./jsmreese/moment-duration-format) – bugovicsb Commented Apr 10, 2018 at 21:35
Add a ment  | 

4 Answers 4

Reset to default 10

If you really want to use the moment-duration-format extension then you need to set the trim option to false.

trim
The default trim behaviour is "large".
Largest-magnitude tokens are automatically trimmed when they have no value.
To stop trimming altogether, set { trim: false }.

console.log(moment.duration(0, 'minutes').format('hh:mm',{ trim: false }))
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.22.0/moment.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/moment-duration-format/2.2.2/moment-duration-format.js"></script>

This will result in 00:00

Remove duration and simply use moment().format() syntax as moment.duration(0, 'minutes').format('HH:mm') will give you error of Uncaught TypeError: moment.duration(...).format is not a function

var res = moment(0, 'minutes').format('HH:mm');
console.log(res);
<script src="https://momentjs./downloads/moment.js"></script>

Try following code

moment.utc(0).format('HH:mm')

If you really want to use format on duration generated. This is the way to do it.

ps. No need for extra moment-duration-format library etc

Thanks

var duration = moment(moment.duration("0")).format("HH:mm");
 

console.log(duration)
<script src="https://momentjs./downloads/moment.js"></script>

发布评论

评论列表(0)

  1. 暂无评论