When a JavaScript/ExtJS Date object is rendered, the date is always relative to the browser.
E.g the timestamp 4102338600000
would be rendered as 'Thu Dec 31 2099 00:00:00 GMT+0530 (India Standard Time)
' with IST zoned browser and will be one day less in EST zoned browser.
I tried couple of ways from the articles on the net like offsetting the timezone, etc but don't see straightforward option.
Our application needs uniform date, say in GMT/UTC/other format, irrespective of timezone.
In app code, 100's of places, below is used to render date :
format : 'd-M-Y',
Is there something to do the same thing but with UTC instead local conversion?
Use toUTCString
:
var date = new Date(4102338600000);
console.log(date.toUTCString());
"Wed, 30 Dec 2099 18:30:00 GMT"
UPDATE
Ext.Date.format
returns local date string representations, indeed. There does not seem to be an out-of-the-box way to make it return UTC dates. You can trick it this way though:
var ms2UTCDate = function(ms) {
var date = new Date(ms);
return new Date(date.valueOf() + 60000 * date.getTimezoneOffset());
}
console.log(Ext.Date.format(ms2UTCDate(4102338600000), 'Y-m-d'));
2099-12-30
That trick can be burned into Ext.Date.format
as follows (don't do this unless you are sure you don't need the original behaviour!):
var originalExtDateFormat = Ext.Date.format;
Ext.Date.format = function(date, format) {
if (date instanceof Date) {
date = new Date(date.valueOf() + 60000 * date.getTimezoneOffset());
}
return originalExtDateFormat.call(this, date, format);
}
Probably you could do below :
- Send the timestamp to server along with the Timezone details.
- On server side, based on the timezone, get the UTC date to save.
- While retrieving, send the timezone as in requested params and have the date fetched accordingly for that.
Little lengthy, but should work :-)