bugovicsb
bugovicsb

Reputation: 456

How to display 0 minute as '00:00' in moment.js?

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

Answers (4)

t.niese
t.niese

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

Shamse Alam
Shamse Alam

Reputation: 1315

Try following code

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

Upvotes: 1

Muhammad Usman
Muhammad Usman

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

Ankit Agarwal
Ankit Agarwal

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

Related Questions