MomentJs和时区缩写 [英] MomentJs and Timezone Abbreviations

查看:167
本文介绍了MomentJs和时区缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在momentjs库中遇到问题

I am having trouble with the momentjs library

线

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

但行

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

我尝试构建格式字符串,但是我认为我需要的zz格式已过时,有一种方法可以使其完全跳过BST/GMT位,因为我只对日期感兴趣

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

先谢谢了.

推荐答案

时区缩写不是唯一的,因此无法解析.您可以通过将任何非格式字符用作占位符来忽略它:

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")

但是您应该意识到,通过忽略它,您将假定运行代码的计算机的本地时区.将您的计算机设置为另一个时区,然后在该时间上调用.format(),您会明白我的意思.

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.

也许您不关心时区,而只想将其重新格式化为其他内容.很好,但是如果您提供的日期由于计算机本地时区的夏令时转换而无效,该怎么办?您的浏览器将向前或向后跳过,具体取决于您所运行的浏览器.为避免这种情况,您应该使用UTC而不是当地时间.即使您的输入值完全来自其他时区,使用UTC仍可确保它不会受到干扰.

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")

这篇关于MomentJs和时区缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆