peter flanagan
peter flanagan

Reputation: 9790

Moment display seconds as 'm:ss'

I am trying to use moment to display 2 seconds like this 0:02.

I expected the following code to work but unfortunately it just displays as0:00. This is based on this section of the docs.

moment(2).format('m:ss');

Can anyone advise where I am going wrong? I have read through the docs and tried numerous approaches with no luck.

Upvotes: 0

Views: 862

Answers (2)

YouneL
YouneL

Reputation: 8361

Try Unix method, it accept an integer value representing the number of seconds since the Unix Epoch (Jan 1 1970 12AM UTC)

Example:

moment.unix(2).format('m:ss');

Demo:

console.log(moment.unix(2).format('m:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>

Upvotes: 0

Alberto Rivera
Alberto Rivera

Reputation: 3752

You do it as

moment().minutes(0).second(2).format('m:ss')

https://jsfiddle.net/4eqL594p/

But this also sets minutes as 0.

EDIT: As another answerer had pointed out, you could also do

moment({seconds: 2}).format('m:ss')

Upvotes: 1

Related Questions