Skip to main content
The 2024 Developer Survey results are live! See the results
Improve the code to make it use long timezone format
Source Link
asiby
  • 3.3k
  • 30
  • 33

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:

const myTimezoneName = "Asia/Colombo";
 
// Generating the formatted text
// Setting the timeZoneName to longOffset will convert PDT to GMT-07:00
const options = {timeZone: myTimezoneName, timeZoneName: "short""longOffset"};
const dateText = Intl.DateTimeFormat([], options).format(new Date);
 
// Scraping the numbers we want from the text
// The default value '+0' is needed when the timezone is missing the number part. Ex. Africa/Bamako --> GMT
let timezoneString = dateText.split(" ")[1].slice(3) || '+0';

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

// Checking for a minutes offset and adding if appropriate
if (timezoneString.includes(":")) {
   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.

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:

const myTimezoneName = "Asia/Colombo";
 
// Generating the formatted text
const options = {timeZone: myTimezoneName, timeZoneName: "short"};
const dateText = Intl.DateTimeFormat([], options).format(new Date);
 
// Scraping the numbers we want from the text
// The default value '+0' is needed when the timezone is missing the number part. Ex. Africa/Bamako --> GMT
let timezoneString = dateText.split(" ")[1].slice(3) || '+0';

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

// Checking for a minutes offset and adding if appropriate
if (timezoneString.includes(":")) {
   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.

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:

const myTimezoneName = "Asia/Colombo";
 
// Generating the formatted text
// Setting the timeZoneName to longOffset will convert PDT to GMT-07:00
const options = {timeZone: myTimezoneName, timeZoneName: "longOffset"};
const dateText = Intl.DateTimeFormat([], options).format(new Date);
 
// Scraping the numbers we want from the text
// The default value '+0' is needed when the timezone is missing the number part. Ex. Africa/Bamako --> GMT
let timezoneString = dateText.split(" ")[1].slice(3) || '+0';

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

// Checking for a minutes offset and adding if appropriate
if (timezoneString.includes(":")) {
   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.

Tweaked the answer to fix a bug and modernized the code
Source Link
asiby
  • 3.3k
  • 30
  • 33

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:

varconst myTimezoneName = "Asia/Colombo";
 
// Generating the formatted text
varconst options = {timeZone: myTimezoneName, timeZoneName: "short"};
varconst dateText = Intl.DateTimeFormat([], options).format(new Date);
 
// Scraping the numbers we want from the text
var// The default value '+0' is needed when the timezone is missing the number part. Ex. Africa/Bamako --> GMT
let timezoneString = dateText.split(" ")[1].slice(3); || '+0';

// Getting the offset
varlet 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.

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.

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:

const myTimezoneName = "Asia/Colombo";
 
// Generating the formatted text
const options = {timeZone: myTimezoneName, timeZoneName: "short"};
const dateText = Intl.DateTimeFormat([], options).format(new Date);
 
// Scraping the numbers we want from the text
// The default value '+0' is needed when the timezone is missing the number part. Ex. Africa/Bamako --> GMT
let timezoneString = dateText.split(" ")[1].slice(3) || '+0';

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

// Checking for a minutes offset and adding if appropriate
if (timezoneString.includes(":")) {
   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.

Source Link

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.