Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJS dates and timezones

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:

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

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?

If this is not possible, can Ext be forced to use GMT rather than trying to interpret timezones?

like image 769
TimS Avatar asked Jan 18 '11 13:01

TimS


1 Answers

I checked the parseDate() implementation in ExtJS source code and 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.

I then checked the source code of getGMTOffset() defined by ExtJS: it builds a time-zone string using the getTimezoneOffset() function defined in JavaScript.

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.

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.

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 Center is relevant here as well:

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.

like image 109
Eric Bréchemier Avatar answered Oct 09 '22 14:10

Eric Bréchemier