I have the following Date brought from my database:
1970-01-01T15:52:00.000Z
I just want the time in UTC. So I use moment
library to do it:
let time = '1970-01-01T15:52:00.000Z'
let _time = moment.utc(time).format("hh:mm")
Since moment
alone converts time into local timestamp (which mine is -03:00), I have to use moment.utc()
to keep the UTC time.
But instead of returning the correct UTC time that is 15:52
, it returns 03:52
.
How can I solve this problem?
CodePudding user response:
I just want the time in UTC. So I use moment library to do it
There are built-in ways of getting UTC time without loading a library.
Use substring to return the parts of the time string you need:
let date = new Date("1970-01-01T15:52:00.000Z");
console.log( date.toUTCString().substring(17,22) );
console.log( date.toJSON().substring(11,16) );
console.log( date.toISOString().substring(11,16) );
CodePudding user response:
hh
means hours on a 12-hour clock. Use HH
instead.
let _time = moment.utc(time).format("HH:mm") // instead of .format("hh:mm")
Thanks, derpirscher, for the answer.