Reputation: 456
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'
?
Upvotes: 4
Views: 4908
Reputation: 40842
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.com/ajax/libs/moment.js/2.22.0/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/2.2.2/moment-duration-format.js"></script>
This will result in 00:00
Upvotes: 10
Reputation: 10148
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.com/downloads/moment.js"></script>
Upvotes: 0
Reputation: 30739
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.com/downloads/moment.js"></script>
Upvotes: 6