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'
?
-
1
moment.duration(0, 'minutes').format('HH:mm')
throws an error becauseformat
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
4 Answers
Reset to default 10If 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>