Ext Date 类似乎为已解析的日期返回了错误的时区,我遇到了问题。使用下面的代码,我为 1966 年 5 月 24 日 15:46 BST 创建了一个日期对象:
date = "1966-05-24T15:46:01+0100";
var pDate = Date.parseDate(date, "Y-m-d\\TH:i:sO", false);
然后我称之为:
console.log(pDate.getGMTOffset());
我希望获得与原始日期关联的偏移量(即 GMT + 1),但我却获得了浏览器的本地时区。如果浏览器设置的时区早于格林威治标准时间足够远,则日期的天部分也将翻转(因此日期现在显示为 1966 年 5 月 25 日)。
有谁知道如何解决这个问题并让 Ext 识别解析日期的正确时区而不是本地浏览器时区?
如果这不可能,是否可以强制 Ext 使用 GMT 而不是尝试解释时区?
最佳答案
我检查了 parseDate() implementation in ExtJS source code和 documentation of Date in core JavaScript , ExtJS 使用的 Date() 构造函数不支持时区信息。 JavaScript Date 对象表示不带时区的 UTC 值。在 ExtJS 源代码解析过程中,时区丢失,而相应的以分钟/秒为单位的偏移量添加到日期。
然后我检查了 source code of getGMTOffset() defined by ExtJS : 它使用 JavaScript 中定义的 getTimezoneOffset() 函数构建时区字符串。
引用documentation of getTimezoneOffset() :
The time-zone offset is the difference between local time and Greenwich Mean Time (GMT). Daylight savings time prevents this value from being a constant.
时区不是存储在 Date 中的变量,它是一个根据 Date 所在年份的时间段而变化的值。
在我的计算机上,使用法语语言环境,
new Date(2010,1,20).getTimezoneOffset()
// -60
new Date(2010,9,20).getTimezoneOffset()
// -120
编辑:此行为并非特定于 ExtJS 中的日期解析,documentation of Date.parse() on Mozilla Doc Center 中的以下注释在这里也相关:
Note that while time zone specifiers are used during date string parsing to properly interpret the argument, they do not affect the value returned, which is always the number of milliseconds between January 1, 1970 00:00:00 UTC and the point in time represented by the argument.
关于javascript - ExtJS 日期和时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4724532/