Hello I am using getTimezoneOffset
an it returns e.g. -60
How could I set global timezone offset for javascript? I tried setTimezoneOffset('-120');
but it is not working. I would like to set it globally at start of the script so later I will be able simply call var d = new Date(); var n = d.getTimezoneOffset();
and it returns my value. Please just solution exactly for this case. Thank you!
Hello I am using getTimezoneOffset
an it returns e.g. -60
How could I set global timezone offset for javascript? I tried setTimezoneOffset('-120');
but it is not working. I would like to set it globally at start of the script so later I will be able simply call var d = new Date(); var n = d.getTimezoneOffset();
and it returns my value. Please just solution exactly for this case. Thank you!
- 1 Use UTC date methods instead of local ones, and subtract the offset when needed. – Oriol Commented Mar 29, 2014 at 0:06
-
2
Basicly
Date.prototype.getTimezoneOffset = function () {return -120;};
does what you need (and exactly for your case only), but what's the point, why not use a regular variable? – Teemu Commented Mar 29, 2014 at 0:15 -
3
JavaScript doesn't really have an option built-in for specifying a timezone to use.
Date
s understand only 2 -- UTC/GMT and "local." And, the latter is determined by the user's system. – Jonathan Lonowski Commented Mar 29, 2014 at 0:19 - 1 Thank you guys! Especially Teemu - it works! :) Sometimes I need to have the same TimezoneOffset for all users. – peter Commented Mar 31, 2014 at 15:44
2 Answers
Reset to default 3Date.prototype.getTimezoneOffset = function () {return -120;};
Using:
// TO ALL dates
Date.timezoneOffset(-240) // +4 UTC
// Override offset only for THIS date
new Date().timezoneOffset(-180) // +3 UTC
Code:
Date.prototype.timezoneOffset = new Date().getTimezoneOffset();
Date.setTimezoneOffset = function(timezoneOffset) {
return this.prototype.timezoneOffset = timezoneOffset;
};
Date.getTimezoneOffset = function(timezoneOffset) {
return this.prototype.timezoneOffset;
};
Date.prototype.setTimezoneOffset = function(timezoneOffset) {
return this.timezoneOffset = timezoneOffset;
};
Date.prototype.getTimezoneOffset = function() {
return this.timezoneOffset;
};
Date.prototype.toString = function() {
var offsetDate, offsetTime;
offsetTime = this.timezoneOffset * 60 * 1000;
offsetDate = new Date(this.getTime() - offsetTime);
return offsetDate.toUTCString();
};
['Milliseconds', 'Seconds', 'Minutes', 'Hours', 'Date', 'Month', 'FullYear', 'Year', 'Day'].forEach((function(_this) {
return function(key) {
Date.prototype["get" + key] = function() {
var offsetDate, offsetTime;
offsetTime = this.timezoneOffset * 60 * 1000;
offsetDate = new Date(this.getTime() - offsetTime);
return offsetDate["getUTC" + key]();
};
return Date.prototype["set" + key] = function(value) {
var offsetDate, offsetTime, time;
offsetTime = this.timezoneOffset * 60 * 1000;
offsetDate = new Date(this.getTime() - offsetTime);
offsetDate["setUTC" + key](value);
time = offsetDate.getTime() + offsetTime;
this.setTime(time);
return time;
};
};
})(this));
Coffee version:
Date.prototype.timezoneOffset = new Date().getTimezoneOffset()
Date.setTimezoneOffset = (timezoneOffset)->
return @prototype.timezoneOffset = timezoneOffset
Date.getTimezoneOffset = (timezoneOffset)->
return @prototype.timezoneOffset
Date.prototype.setTimezoneOffset = (timezoneOffset)->
return @timezoneOffset = timezoneOffset
Date.prototype.getTimezoneOffset = ->
return @timezoneOffset
Date.prototype.toString = ->
offsetTime = @timezoneOffset * 60 * 1000
offsetDate = new Date(@getTime() - offsetTime)
return offsetDate.toUTCString()
[
'Milliseconds', 'Seconds', 'Minutes', 'Hours',
'Date', 'Month', 'FullYear', 'Year', 'Day'
]
.forEach (key)=>
Date.prototype["get#{key}"] = ->
offsetTime = @timezoneOffset * 60 * 1000
offsetDate = new Date(@getTime() - offsetTime)
return offsetDate["getUTC#{key}"]()
Date.prototype["set#{key}"] = (value)->
offsetTime = @timezoneOffset * 60 * 1000
offsetDate = new Date(@getTime() - offsetTime)
offsetDate["setUTC#{key}"](value)
time = offsetDate.getTime() + offsetTime
@setTime(time)
return time