最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert from GMTUTC to local time and back using momentjs? - Stack Overflow

programmeradmin1浏览0评论

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?

When I do:

moment(myDateString).local()

Then later to try to convert back:

moment(myDateStrig).utc()

They both yield different results. How do I make it so that I can convert the time I have from UTC to local, then back to UTC?

Note: When I try the .utc() function on the string "December 25, 2018 01:02:20" I get a different time. When I do a .local() on the utc converted moment, I get a different date that is not my original date of "December 25, 2018 01:02:20".

Please show an example if you can as I it looks like I have 3 different dates.

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?

When I do:

moment(myDateString).local()

Then later to try to convert back:

moment(myDateStrig).utc()

They both yield different results. How do I make it so that I can convert the time I have from UTC to local, then back to UTC?

Note: When I try the .utc() function on the string "December 25, 2018 01:02:20" I get a different time. When I do a .local() on the utc converted moment, I get a different date that is not my original date of "December 25, 2018 01:02:20".

Please show an example if you can as I it looks like I have 3 different dates.

Share Improve this question edited Apr 27, 2018 at 23:19 VincenzoC 31.5k12 gold badges100 silver badges121 bronze badges asked Apr 27, 2018 at 21:04 RolandoRolando 62.8k104 gold badges281 silver badges423 bronze badges 5
  • "December 25, 2018 01:02:20" does not have a time zone, so it will be interpreted as a local time. Then, calling utc() will convert it to UTC time. – laptou Commented Apr 27, 2018 at 21:06
  • Possible duplicate of Convert date to UTC using moment.js – Heretic Monkey Commented Apr 27, 2018 at 21:12
  • 1 This is covered in the documentation... – Heretic Monkey Commented Apr 27, 2018 at 21:14
  • I have looked at those, and they don't seem to match my problem. PLease note the date string I am working with..."December 25, 2018 01:02:20". When I do the utc() it changes it to something else. When I use .local() it does not match "December 25, 2018 01:02:20" – Rolando Commented Apr 27, 2018 at 21:19
  • you should look for this stackoverflow./questions/33321495/… – Lalit Mohan Commented Feb 11, 2021 at 10:20
Add a ment  | 

2 Answers 2

Reset to default 1

Since your input represent UTC time, you have to use moment.utc (as suggested by others in the ments):

By default, moment parses and displays in local time.

If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().

Moreover, since your input string is not in a format recognized by moment(String) (ISO 8601 or RFC 2822), you have to pass format parameter as second argument of moment.utc, see String + Format parsing section of the docs.

Here a code sample:

var input = "December 25, 2018 01:02:20";
var fmt = 'MMMM DD, YYYY HH:mm:ss';
var m = moment.utc(input, fmt);
console.log(m.local().format(fmt));
console.log(m.utc().format(fmt));
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.21.0/moment.min.js"></script>

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 remend specifying a format in order to alleviate ambiguity.

发布评论

评论列表(0)

  1. 暂无评论