I have the following string:
\"December 25, 2018 01:02:20\"
I intend this to be GMT/UTC Using moment.js, how do I convert this to local time and back to UTC?
In order to interpret a string such as "December 25, 2018 01:02:20" which does not have a time zone on it, you can call moment()
on it, which will interpret it as local time, or you can call moment.utc() on it, which will interpret it as UTC time.
Try this:
const myUtcMoment = moment.utc(myDateString)
This should result in a Moment
object that represents 01:02:20 AM on December 25, 2018 in the UTC time zone.
In order to convert that Moment
to local time, you can do this:
const myLocalMoment = myUtcMoment.local()
And to convert it back, you can call myLocalMoment.utc()
.
By the way, with this date format, it is not really clear whether this is 24-hour time or not. The Moment.js docs recommend specifying a format in order to alleviate ambiguity.