Javascript Date Set Timezone

Posted on  by admin

The getTimezoneOffset method returns the time zone difference, in minutes, from current locale (host system settings) to UTC.

The getTimezoneOffset() method returns the difference, in minutes, between a date as evaluated in the UTC time zone, and the same date as evaluated in the local time zone.

Syntax

  1. Next, find the local time zone offset with the Date object's getTimezoneOffset method. By default, this method returns the time zone offset in minutes, so convert this value to milliseconds.
  2. A JavaScript Date and Time Library. Contribute to datejs/Datejs development by creating an account on GitHub. // Returns today's date, with time set to 00:00.
  3. The setUTCDate method changes the day of the month of a given Date instance, based on UTC time. To instead change the day of the month for a given Date instance based on local time.
  4. When a Date object is created, a number of methods allow you to operate on it. Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of date objects, using either local time or UTC (universal, or GMT) time.

Return value

Javascript date adjust timezone

Javascript Date Set Timezone Global

The difference, in minutes, between date, as evaluated in the UTC time zone, and as evaluated in the local time zone.

Description

date.getTimezoneOffset() returns the difference, in minutes, between date as evaluated in the UTC time zone, and date as evaluated in the local time zone — that is, the time zone of the host system in which the browser is being used (if the code is run from the Web in a browser), or otherwise the host system of whatever JavaScript runtime (for example, a Node.js environment) the code is executed in.

Negative values and positive values

The number of minutes returned by getTimezoneOffset() is positive if the local time zone is behind UTC, and negative if the local time zone is ahead of UTC. For example, for UTC+10, -600 will be returned.

Current time zoneUTC-8UTCUTC+3
Return Value4800-180

Javascript Date Adjust Timezone

Varied results in Daylight Saving Time (DST) time zones

Javascript Date Set Timezone

In a time zone that annually shifts in and out of Daylight Saving Time (DST), the number of minutes returned by calling getTimezoneOffset() can vary.

Consider a given local time zone and a date date1 that are both in DST, and consider minutes, the number of minutes returned by calling date1.getTimezoneOffset(); then:

  • If the local time zone is currently in DST, but a given date date2 is not in DST, then the number of minutes returned by date2.getTimezoneOffset() is minutes ± 60.
  • If the local time zone is not currently in DST, but a given date date3 is in DST, then the number of minutes returned by date3.getTimezoneOffset() is minutes ± 60.

In a time zone that doesn’t annually shift in and out of Daylight Saving Time (DST), the number of minutes returned by calling getTimezoneOffset() always returns the same number of minutes, regardless of the date instance it’s called from.

Note: The above description is a simplification. In implementations, the IANA time zone database (tzdata) is used for precisely determining the effect of DST on the calculation of the time-zone difference.

Examples

Specifications

Specification
ECMAScript Language Specification (ECMAScript)
#sec-date.prototype.gettimezoneoffset

Browser compatibility

BCD tables only load in the browser

See also