4

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!

asked Mar 28, 2014 at 23:26
4
  • 1
    Use UTC date methods instead of local ones, and subtract the offset when needed. 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? Commented Mar 29, 2014 at 0:15
  • 3
    JavaScript doesn't really have an option built-in for specifying a timezone to use. Dates understand only 2 -- UTC/GMT and "local." And, the latter is determined by the user's system. 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. Commented Mar 31, 2014 at 15:44

2 Answers 2

3
Date.prototype.getTimezoneOffset = function () {return -120;};
answered Mar 31, 2014 at 15:45
Sign up to request clarification or add additional context in comments.

2 Comments

new Date instances don't seem to take this into account though
Date.prototype.getTimezoneOffset = function () {return -520}; hours1 = new Date().getHours(); Date.prototype.getTimezoneOffset = function () {return -120}; hours2 = new Date().getHours(); hours1 === hours2 // true
2

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
Philippe
4,0613 gold badges44 silver badges57 bronze badges
answered Nov 10, 2016 at 1:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.