javascript ExtJS 日期和时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4724532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ExtJS dates and timezones
提问by TimS
I have a problem with the Ext Date class seemingly returning the wrong timezone for a parsed date. Using the code below I create a date object for the 24th May, 1966 15:46 BST:
Ext Date 类似乎为解析的日期返回了错误的时区,我遇到了问题。使用下面的代码,我为英国夏令时 1966 年 5 月 24 日 15:46 创建了一个日期对象:
date = "1966-05-24T15:46:01+0100";
var pDate = Date.parseDate(date, "Y-m-d\TH:i:sO", false);
I then call this:
然后我称之为:
console.log(pDate.getGMTOffset());
I am expecting to get the offset associated with the orignal date back (which is GMT + 1), but instead I get the local timezone of the browser instead. If the browser is set to a timezone far enough ahead GMT, the day portion of the date will also be rolled over (so the date will now appear as 25th May, 1966).
我希望得到与原始日期相关的偏移量(即 GMT + 1),但我得到的是浏览器的本地时区。如果浏览器设置的时区比 GMT 早得多,日期的天部分也将滚动(因此日期现在将显示为 1966 年 5 月 25 日)。
Does anyone know how to get around this and get Ext to recognise the correct timezone of the parsed date rather than the local browser timezone?
有谁知道如何解决这个问题并使 Ext 识别解析日期的正确时区而不是本地浏览器时区?
If this is not possible, can Ext be forced to use GMT rather than trying to interpret timezones?
如果这是不可能的,是否可以强制 Ext 使用 GMT 而不是尝试解释时区?
回答by Eric Bréchemier
I checked the parseDate() implementation in ExtJS source codeand the documentation of Date in core JavaScript, the Date() constructor used by ExtJS does not support time zone information. JavaScript Date objects represent a UTC value, without the time zone. During parsing in ExtJS source code, the time zone is lost while the corresponding offset in minutes/seconds is added to the Date.
我检查了ExtJS 源代码中的parseDate() 实现和核心 JavaScript 中 Date的文档,ExtJS 使用的 Date() 构造函数不支持时区信息。JavaScript Date 对象表示一个 UTC 值,没有时区。在 ExtJS 源代码中解析时,时区丢失,而相应的以分钟/秒为单位的偏移量添加到日期。
I then checked the source code of getGMTOffset() defined by ExtJS: it builds a time-zone string using the getTimezoneOffset() function defined in JavaScript.
然后我检查了ExtJS 定义的 getGMTOffset()的源代码:它使用 JavaScript 中定义的 getTimezoneOffset() 函数构建一个时区字符串。
Quoting the 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.
时区偏移是本地时间和格林威治标准时间 (GMT) 之间的差异。夏令时防止该值成为常数。
The time-zone is not a variable stored in the Date, it is a value that varies according to the period of the year that the Date falls in.
时区不是存储在 Date 中的变量,它是一个根据 Date 所在年份的时间段而变化的值。
On my computer, with a French locale,
在我的电脑上,法语语言环境,
new Date(2010,1,20).getTimezoneOffset()
// -60
new Date(2010,9,20).getTimezoneOffset()
// -120
Edit: this behavior is not specific to Date parsing in ExtJS, the following note in the documentation of Date.parse() on Mozilla Doc Centeris relevant here as well:
编辑:此行为并非特定于 ExtJS 中的日期解析,Mozilla Doc Center 上 Date.parse() 文档中的以下注释也与此处相关:
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.
请注意,虽然在日期字符串解析期间使用时区说明符来正确解释参数,但它们不会影响返回的值,该值始终是 1970 年 1 月 1 日 00:00:00 UTC 和所表示的时间点之间的毫秒数由论据。
回答by Nick
I'm a little late but in latest ExtJS, you can pass an optional boolean to prevent the "rollover" in JS
我有点晚了但是在最新的 ExtJS 中,您可以传递一个可选的布尔值来防止 JS 中的“翻转”
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Date-method-parse
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Date-method-parse
回答by X2theZ
My two cents, because I can't really set all my time to 12:00 like Tim did. I posted on the sencha forum
我的两分钱,因为我真的不能像蒂姆那样把所有时间都设置为 12:00。我在sencha论坛上发帖

