MomentJs and Timezone Abbreviations

后端 未结 1 1876
日久生厌
日久生厌 2021-01-13 07:12

I am having trouble with the momentjs library

the line

moment(\"Mon Oct 14 01:00:00 GMT 2013\")  parses correctly 

but the line

1条回答
  •  生来不讨喜
    2021-01-13 07:50

    Time zone abbreviations aren't unique, so they cannot be parsed. You can ignore it by putting any non-format character as a placeholder:

    moment("Mon Oct 14 01:00:00 BST 2013","ffffd MMM DD HH:mm:ss ? YYYY")
    

    But you should be aware that by ignoring it, you'll be assuming the local time zone of the computer where the code is running. Set your computer for another time zone and call .format() on this and you'll see what I mean.

    Perhaps you don't care about time zones and just want to reformat this to something else. That's fine, but what if you provide a date that's invalid because of a daylight saving time transition in the computer's local time zone? Your browser will either skip backward or forward depending on which browser your running. To avoid that, you should work in UTC instead of in the local time. Even though your input value is from some other time zone entirely, working in UTC will ensure it doesn't get mangled.

    moment.utc("Mon Oct 14 01:00:00 BST 2013","ffffd MMM DD HH:mm:ss ? YYYY")
    

    0 讨论(0)
提交回复
热议问题