Albert
Albert

Reputation: 2664

how to convert epoch time in milliseconds to formatted date using momentjs

I do this

const d = moment(1623253727754);
console.log(d.format('YYYY-MM-DD HH:MM:SS'));

and I get this

2021-06-09 18:06:75

What on Earth is this date? How does it make it 75 seconds?

Upvotes: 0

Views: 98

Answers (1)

amda
amda

Reputation: 354

Because according to momentjs docs, 'SS' in CAPITAL S represents fractional seconds, not actual seconds. Try d.format('YYYY-MM-DD HH:MM:ss'). Or you could also avoid importing momentjs totally by simply using Javascript's Date method, toISOString:

const d = new Date(1623253727754);
console.log(d.toISOString());

Upvotes: 2

Related Questions