How do I set the time zone in javascript? (set timezone offset javascript Date)

The browser rigidly sets the time zone of the date objects based on the time zone settings of the operating system. But in some applications, it is necessary that all users of the site see literally the same time, from whatever place on the globe they would not go. How can I manually set the time zone offset in javascript? So that it is automatically applied to all Date objects?

Author: Maxmaxmaximus, 2016-11-10

2 answers

Use the moment library.js.

In it, this is done easily and simply:

var message = "Московское время: " + moment().utcOffset(3).format("HH:mm")
 2
Author: Pavel Mayorov, 2016-11-10 14:50:51

I wrote a cool polyfill especially for myself and you! Adds the much-coveted Date.setTimezoneOffset() function . Surprisingly, this is not initially built into the browser and javascript. Here it is, you can like it: 3 https://github.com/uMaxmaxmaximus/date-timezone

Usage example:

// Установить смещение таймзоны для всех дат
Date.timezoneOffset(-240) // +4 UTC

// Установить смещение таймзоны только для данной даты
(new Date).timezoneOffset(-180) // +3 UTC

Coffeescript version:

do ->   
    # tmp date
    offsetDate = new Date()

    # default timezone
    Date.prototype.timezoneOffset = offsetDate.getTimezoneOffset()


    Date.setTimezoneOffset = (timezoneOffset)->
        return @prototype.timezoneOffset = timezoneOffset


    Date.getTimezoneOffset = (timezoneOffset)->
        return @prototype.timezoneOffset


    Date.prototype.getTimezoneOffset = ->
        return @timezoneOffset


    Date.prototype.setTimezoneOffset = (timezoneOffset)->
        return @timezoneOffset = timezoneOffset


    Date.prototype.toString = ->
        offsetTime = @timezoneOffset * 60 * 1000
        offsetDate.setTime(@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.setTime(@getTime() - offsetTime)
            return offsetDate["getUTC#{key}"]()

        Date.prototype["set#{key}"] = (value)->
            offsetTime = @timezoneOffset * 60 * 1000
            offsetDate.setTime(@getTime() - offsetTime)
            offsetDate["setUTC#{key}"](value)
            time = offsetDate.getTime() + offsetTime
            @setTime(time)
            return time

Compiled version:

(function() {
  var offsetDate;
  offsetDate = new Date();
  Date.prototype.timezoneOffset = offsetDate.getTimezoneOffset();
  Date.setTimezoneOffset = function(timezoneOffset) {
    return this.prototype.timezoneOffset = timezoneOffset;
  };
  Date.getTimezoneOffset = function(timezoneOffset) {
    return this.prototype.timezoneOffset;
  };
  Date.prototype.getTimezoneOffset = function() {
    return this.timezoneOffset;
  };
  Date.prototype.setTimezoneOffset = function(timezoneOffset) {
    return this.timezoneOffset = timezoneOffset;
  };
  Date.prototype.toString = function() {
    var offsetTime;
    offsetTime = this.timezoneOffset * 60 * 1000;
    offsetDate.setTime(this.getTime() - offsetTime);
    return offsetDate.toUTCString();
  };
  return ['Milliseconds', 'Seconds', 'Minutes', 'Hours', 'Date', 'Month', 'FullYear', 'Year', 'Day'].forEach((function(_this) {
    return function(key) {
      Date.prototype["get" + key] = function() {
        var offsetTime;
        offsetTime = this.timezoneOffset * 60 * 1000;
        offsetDate.setTime(this.getTime() - offsetTime);
        return offsetDate["getUTC" + key]();
      };
      return Date.prototype["set" + key] = function(value) {
        var offsetTime, time;
        offsetTime = this.timezoneOffset * 60 * 1000;
        offsetDate.setTime(this.getTime() - offsetTime);
        offsetDate["setUTC" + key](value);
        time = offsetDate.getTime() + offsetTime;
        this.setTime(time);
        return time;
      };
    };
  })(this));
})();
 0
Author: Maxmaxmaximus, 2016-11-10 14:32:37