Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get timezone offset from timezone name using Javascript

I found many solution that gives Timezone name from offset value. But I have Timezone name and I want offset value for that. I tried setTimezone('Asia/Kolkata'), but I think their is no method like setTimezone.

example:

Asia/Kolkata should give me -330 ( offset )
like image 800
Hardik Sondagar Avatar asked Jan 24 '14 08:01

Hardik Sondagar


People also ask

How do you get timezone name from timezone offset?

Copied! The getTimezoneOffset method returns the difference, in minutes, between a date (evaluated in UTC) and the same date evaluated in the visitor's local time zone. If you get a value like -120 , then the time zone offset is UTC+02. Similarly, for a value of -60 , the time zone offset is UTC+01.

How do I get a time zone offset?

Definition and Usage. getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.

How can I get the timezone name in JavaScript?

In javascript , the Date. getTimezoneOffset() method returns the time-zone offset from UTC, in minutes, for the current locale.


2 Answers

This has got the be the easiest way to accomplish this task with modern JavaScript.

Note: Keep in mind that the offset is dependent on whether Daylight Savings Time (DST) is active.

/* @return A timezone offset in minutes */
const getOffset = (timeZone = 'UTC', date = new Date()) => {
  const utcDate = new Date(date.toLocaleString('en-US', { timeZone: 'UTC' }));
  const tzDate = new Date(date.toLocaleString('en-US', { timeZone }));
  return (tzDate.getTime() - utcDate.getTime()) / 6e4;
}

console.log(`No arguments: ${getOffset()}`); // 0

{
  console.log('! Test Case #1 >> Now');
  console.log(`Asia/Colombo     : ${getOffset('Asia/Colombo')}`);     //  330
  console.log(`America/New_York : ${getOffset('America/New_York')}`); // -240
}

{
  console.log('! Test Case #2 >> DST : off');
  const date = new Date(2021, 0, 1);
  console.log(`Asia/Colombo     : ${getOffset('Asia/Colombo', date)}`);     //  330
  console.log(`America/New_York : ${getOffset('America/New_York', date)}`); // -300
}

{
  console.log('! Test Case #3 >> DST : on');
  const date = new Date(2021, 5, 1);
  console.log(`Asia/Colombo     : ${getOffset('Asia/Colombo', date)}`);     //  330
  console.log(`America/New_York : ${getOffset('America/New_York', date)}`); // -240
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
like image 135
Mr. Polywhirl Avatar answered Sep 20 '22 06:09

Mr. Polywhirl


I came across this same issue, and this is the solution I came up with, if you can get an IANA tz database name like the one you mentioned:

var myTimezoneName = "Asia/Colombo";
 
// Generating the formatted text
var options = {timeZone: myTimezoneName, timeZoneName: "short"};
var dateText = Intl.DateTimeFormat([], options).format(new Date);
 
// Scraping the numbers we want from the text
var timezoneString = dateText.split(" ")[1].slice(3);

// Getting the offset
var timezoneOffset = parseInt(timezoneString.split(':')[0])*60;

// Checking for a minutes offset and adding if appropriate
if (timezoneString.includes(":")) {
    var timezoneOffset = timezoneOffset + parseInt(timezoneString.split(':')[1]);
}

It's not a very nice solution, but it does the job without importing anything. It relies on the output format of the Intl.DateTimeFormat being consistent, which it should be, but that's a potential caveat.

like image 24
Baptiste Higgs Avatar answered Sep 20 '22 06:09

Baptiste Higgs