Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MomentJs and Timezone Abbreviations

Tags:

momentjs

I am having trouble with the momentjs library

the line

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

but the line

moment("Mon Oct 14 01:00:00 BST 2013")  throws an invalid date

I have tried building a format string but the zz format which is what I think I need is deprecated, is there a way to make it skip the BST/GMT bit completely as I am only interested in the date

Thanks in advance.

like image 935
user3185608 Avatar asked Oct 21 '22 17:10

user3185608


1 Answers

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","ddd 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","ddd MMM DD HH:mm:ss ? YYYY")
like image 127
Matt Johnson-Pint Avatar answered Nov 17 '22 12:11

Matt Johnson-Pint