Get timezone offset from timezone name using Javascript - 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 )

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; }

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.

You can't get it by name alone. You would also need to know the specific time. Asia/Kolkata may be fixed to a single offset, but many time zones alternate between standard time and daylight saving time, so you can't just get the offset, you can only get an offset.
For how to do it in JavaScript, see this answer.

Using countries and timezones npm package:
import {getTimezone} from 'countries-and-timezones';
const australianTimezone = 'Australia/Melbourne';
console.log(getTimezone(australianTimezone));
Prints to the console:
{
name: 'Australia/Melbourne',
country: 'AU',
utcOffset: 600,
utcOffsetStr: '+10:00',
dstOffset: 660,
dstOffsetStr: '+11:00',
aliasOf: null
}
From there, you can use the utcOffset or dstOffset depending on if it is daylight savings time.

Related

How can I make the getTime() method aware of the local time?

I am working on displaying a banner on a website, between two dates (the campaign's start date and end date).
For this purpose, I have the following function:
function checkCampaignActive() {
const campaignDateStart = new Date('2022-09-14');
const campaignDateStop = new Date('2022-09-15');
const today = Date.now();
const isCampaignStarted = today >= campaignDateStart.getTime();
const isCampaignExpired = today > campaignDateStop.getTime();
this.isCampaignActive = isCampaignStarted && !isCampaignExpired;
console.log('Is started: ', isCampaignStarted);
console.log('Is expired: ', isCampaignExpired);
console.log(isCampaignActive);
}
checkCampaignActive();
The problem
Since getTime() returns the milliseconds between Jan 1 1970, 00:00 GMT and 2022-09-14 and 00:00 GMT and my local time is not GMT, there is a gap between the desired campaign start and the real one.
Whatever new Date('2022-09-14') returns has to be adjusted for the local time, but remain an object, not a string.
What is the easiest way to fix this issue?
The cause of the "problem" is that the date string your code provides to the Date constructor is interpreted as a simplified ISO 8601 format, denoting UTC.
The best way is to avoid a string typed argument, and provide numeric arguments. The equivalent of your example would be:
const campaignDateStart = new Date(2022, 8, 14);
const campaignDateStop = new Date(2022, 8, 15);
(Be aware of the zero-based month numbering)
It is possible to get away with it using strings. For instance, these options work because they step away from the format that JavaScript will interpret as UTC:
Append a time specification (without appending "Z" or other time zone specifier);
const campaignDateStart = new Date('2022-09-14 00:00');
const campaignDateStop = new Date('2022-09-15 00:00');
As stated by Mozilla Contributors:
Date-only strings (e.g. "1970-01-01") are treated as UTC, while date-time strings (e.g. "1970-01-01T12:00") are treated as local.
Or use a different delimiter:
const campaignDateStart = new Date('2022/09/14');
const campaignDateStop = new Date('2022/09/15');
But these rely on the complex rules of how strings are parsed as dates. Some of that logic is host-dependent, so in general it is better to avoid passing strings to the date constructor (or to Date.parse which uses the same logic for string arguments).
A small change to your code will make it work the way you wish.
When you call new Date('2022-09-14') it returns the number of milliseconds from 1970-01-01 00:00:00 UTC to 2022-09-14 00:00:00 UTC, however you actually with to get the number of milliseconds from 1970-01-01 00:00:00 UTC
2022-09-14 00:00:00 [local timezone].
It's easy to make this change, simply parse '2022-09-14 00:00:00' instead, and the same with the subsequent date. This will return the expected values and give the correct answer.
function checkCampaignActive() {
const campaignDateStart = new Date('2022-09-14 00:00');
const campaignDateStop = new Date('2022-09-15 00:00');
const today = Date.now();
const isCampaignStarted = today >= campaignDateStart.getTime();
const isCampaignExpired = today > campaignDateStop.getTime();
this.isCampaignActive = isCampaignStarted && !isCampaignExpired;
console.log('Is started: ', isCampaignStarted);
console.log('Is expired: ', isCampaignExpired);
console.log(isCampaignActive);
}
checkCampaignActive();
.as-console-wrapper { max-height: 100% !important; }
Visit this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
Add GMT in Date to add your timezone. Then use .getTimezoneOffset to get gap in Minutes.
function checkCampaignActive() {
const campaignDateStart = new Date('2022-09-14 GMT+5:45');
const campaignDateStop = new Date('2022-09-15 GMT+5:45');
const today = Date.now();
const today1 = Date.now() + campaignDateStart.getTimezoneOffset() * 60;
const isCampaignStarted = today >= campaignDateStart.getTime();
const isCampaignExpired = today > campaignDateStop.getTime();
this.isCampaignActive = isCampaignStarted && !isCampaignExpired;
console.log('Is started: ', isCampaignStarted);
console.log('Is expired: ', isCampaignExpired);
console.log(isCampaignActive);
console.log(today);
console.log(today1);
console.log(campaignDateStart.getTimezoneOffset());
}
checkCampaignActive();

How can I get the timestamp in javascript from a string including the timezone db name

I got the following string: "2022/05/01 03:10:00" and I need to create a Date object forcing it to use Chile's UTC offset.
The problem is that because of Daylight saving time (DST) the offset changes twice a year.
How can get that Date object, for example, using the "America/Santiago" TZ db name?
Something like:
new Date("2022/05/01 03:10:00" + getUtcOffset("America/Santiago")).
function getUtcOffset(tzDbName) {
..
}
Returns -3 or -4, depending the time in the year.
EDIT:
I ended using a nice trick for determining if DST was on or off.
reference
const dst = hasDST(new Date(strDate));
function hasDST(date = new Date()) {
const january = new Date(date.getFullYear(), 0, 1).getTimezoneOffset();
const july = new Date(date.getFullYear(), 6, 1).getTimezoneOffset();
return Math.max(january, july) !== date.getTimezoneOffset();
}
Then I could create the date with the correct timezone depending on that variable.
if (dst) {
let d = new Date(strDate + " GMT-0300");
return d;
} else {
let d = new Date(strDate + " GMT-0400");
return d;
}
Thanks everyone!
EDIT2:
I finally found a very nice library that does exactly what I was looking for:
https://date-fns.org/v2.28.0/docs/Time-Zones#date-fns-tz
const { zonedTimeToUtc, utcToZonedTime, format } = require('date-fns-tz')
const utcDate = zonedTimeToUtc('2022-05-05 18:05', 'America/Santiago')
This has been discussed before here.
Haven't tested it, but it appears that the simplest solution is:
// Example for Indian time
let indianTime = new Date().toLocaleTimeString("en-US",
{timeZone:'Asia/Kolkata',timestyle:'full',hourCycle:'h24'})
console.log(indianTime)
You can check the link for more complex answers and libraries
Generals notes
To get the time zone name use:
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
To get the difference from UTC (in minutes) use:
var offset = new Date().getTimezoneOffset();
console.log(offset);
// if offset equals -60 then the time zone offset is UTC+01

Timezone offset by timezone name for a specific date in Javascript

How can I find timezone offset in hours or minutes using timezone name for a specific date.
Example use would be :
var offset = findOffset("America/New_York", new Date(2019,08,07))
// offset is -4
I am hoping to find a solution using native JS, but if it does not exist I am also ok with using some libraries.
I will recommend Moment Timezone which is a JS library and it is quite popular (3,660,521 weeks NPM downloads). Moment and Moment Timezone are sibling libraries (Same developer).
The moment timezone library gives you offset in the output like shown below:
moment.tz("2013-12-01", "America/Los_Angeles").format(); // 2013-12-01T00:00:00-08:00
moment.tz("2013-06-01", "America/Los_Angeles").format(); // 2013-06-01T00:00:00-07:00
So here is the answer I found
const findTimeZoneOffset = (tz,date) => {
let utcDate = new Date(date.toLocaleString('en-US', { timeZone: "UTC" }));
let tzDate = new Date(date.toLocaleString('en-US', { timeZone: tz }));
let diff = ( tzDate.getTime() - utcDate.getTime() ) / 1000 / 60 / 60;
return diff;
};
and as expected
findTimeZoneOffset("America/New_York", new Date(2019,08,07))
// returns -4

Easiest way to convert UTC to New Zealand date (account for daylight savings)?

I have the following function which I've written to convert msSinceEpoch to the New Zealand Date (IE11 Compatible)...
const MAGICNUMBER = 13;
const toNewZealand = (msSinceEpoch) => {
const [day, month, year, time] = new Date(msSinceEpoch).toLocaleString("en-NZ", {
hour12: false, timeZone: "UTC"
}).split(/[/,]/); // timeZone UTC is the only format support on IE11
const [hours, minutes, seconds] = time.trim().split(":");
return new Date(~~year, ~~month - 1, ~~day, ~~hours + MAGICNUMBER, ~~minutes, ~~seconds)
};
// usage....
console.log(
toNewZealand(new Date().getTime())
)
However, this contains a magic number which is not relative to New Zealand's daylight savings time (+12 or +13).
So here it gets complicated, how do I get the right number relative to daylight savings in New Zealand (+12 or +13).
My initial attempt was just to calculate whether it was in between the last Sunday of September or first Sunday of April but then I realised that the second I use a new Date() constructor anywhere in the code it's going to create a date relative to their system time and break the math.
TL;DR Convert UTC Milliseconds since epoch to New Zealand Time that works with New Zealand's Daylight savings settings.
EDIT: Also not interested in using Moment or any other library to solve this problem due to bundle size costs.
TL;DR Convert UTC Milliseconds since epoch to New Zealand Time...
I think OP has a misunderstanding of what time conversion is. MS Since Epoch are always in UTC. Time conversation changes the display format of the time, and should not change msSinceEpoch
You can use toLocalString() and pass the desired timezone (Pacific/Auckland) to convert the display format of your DateTime:
const date = new Date();
console.log(
date.toLocaleString('en-NZ', {
timeZone: 'Pacific/Auckland',
}),
);
Solved it, tried to keep it as human readable as possible, basically tries to add 12 or 13 depending on where the current UTC DateTime lies, if by adding 12 or 13 we fall into the next daylight savings period we add the alternate instead....
IE if by adding 12 we fall into +13 territory.... add +13 instead.
IE if by adding +13 we fall into +12 territory.... add +12 instead.
New Zealand's daylight savings time changes on the last sunday of september and the first sunday of April.
This is the solution....
const UTCFromMS = (ms) => {
return new Date(new Date(ms).toUTCString().replace(" GMT", ""))
};
const addHours = (dte, hrs) => {
return new Date(
dte.getFullYear(),
dte.getMonth(),
dte.getDate(),
dte.getHours() + hrs,
dte.getMinutes(),
dte.getMilliseconds()
);
};
const toNewZealand = (ms) => {
return addNewZealandDaylightSavings(UTCFromMS(ms));
};
const getPreviousSunday = (dte) => {
return new Date(
dte.getFullYear(),
dte.getMonth(),
dte.getDate() - dte.getDay(),
1,
0,
0
);
};
const getNextSunday = (dte) => {
return new Date(
dte.getFullYear(),
dte.getMonth(),
dte.getDay() === 0 ? dte.getDate() : dte.getDate() + (7 - dte.getDay()),
1,
0,
0
)
};
const standardHours = 12;
const daylightHours = 13;
const addNewZealandDaylightSavings = (dte) => {
const lastSundaySeptember = getPreviousSunday(
new Date(dte.getFullYear(), 8, 30)
);
const firstSundayApril = getNextSunday(
new Date(dte.getFullYear(), 3, 1)
);
// If its before firstSundayApril, add 13, if we went over 1am, add 12.
if(dte <= firstSundayApril) {
const daylightNz = addHours(dte, daylightHours);
if(daylightNz >= firstSundayApril) {
return addHours(dte, standardHours);
}
return daylightNz
}
// if its before lastSundaySeptember, add 12 if we went over 1am add 13.
if(dte <= lastSundaySeptember) {
const standardNz = addHours(dte, standardHours);
if(standardNz >= lastSundaySeptember) {
return addHours(dte, daylightHours);
}
return standardNz;
}
return addHours(dte, daylightHours);
};
console.log(toNewZealand(new Date().getTime()).toString());
// the above line should always output the current DateTime in New Zealand, replace the argument with any epoch milliseconds and it should still always give you the correct time.
===EDIT====
The above answer has since become less relevant as now there's way to do this that are part of the web standard.
Date.prototype.toLocaleString AND timeZone: "Pacific/Auckland"
Is one of the simplest way's to convert dates today, you can read about it more on MDN here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

how to get time zone of city or country in javascript

I use getTimezoneOffset() to get the offset in minutes for a given date object. Is is possible to use javascript to get timezone offset of a city or a country?
for example:
var offset = getCityOffset("Miami"); // returns -240
No, there is nothing built-in to javascript which allows you to get the number of minutes to offset for specific time zones/cities.
getTimeZoneOffset works for the current browser's settings
MomentJS Timezone extensions has some of this sort of functionality, which is of course reliant on the MomentJS library.
If you have access to Lat/Long values, then google provide a timezone API
I'm going to answer this question for Node.js using TypeScript (please remove the types if you want to use with plan JavaScript). For that, we will need 2 NPM packages.
moment-timezone
city-timezones
Disclaimer: Timezones are COMPLEX. Not all timezones are 1 hour apart and some daylight saving time settings are weird with, for example, 15 minutes difference instead of the standard 60 minutes. This is a naive implementation that suited my use case. Use with discretion.
Code:
import * as cityTimeZones from "city-timezones";
import * as moment from "moment-timezone";
/**
* Returns the UTC offset for the given timezone
* #param timezone Example: America/New_York
*/
export function getNormalizedUtcOffset(timezone: string): number | null {
const momentTimezone = moment.tz(timezone);
if (!momentTimezone) {
return null;
}
let offset = momentTimezone.utcOffset();
if (momentTimezone.isDST()) {
// utcOffset will return the offset normalized by DST. If the location
// is in daylight saving time now, it will be adjusted for that. This is
// a NAIVE attempt to normalize that by going back 1 hour
offset -= 60;
}
return offset/60;
}
/**
* Returns the offset range for the given city or region
* #param location
*/
export function getUtcOffsetForLocation(location: string): number[] | null {
const timezones = cityTimeZones.findFromCityStateProvince(location);
if (timezones && timezones.length) {
// timezones will contain an array of all timezones for all cities inside
// the given location. For example, if location is a country, this will contain
// all timezones of all cities inside the country.
// YOU SHOULD CACHE THE RESULT OF THIS FUNCTION.
const offsetSet = new Set<number>();
for (let timezone of timezones) {
const offset = getNormalizedUtcOffset(timezone.timezone);
if (offset !== null) {
offsetSet.add(offset);
}
}
return [...offsetSet].sort((a, b) => a - b);
}
return null;
}
Unit tests (with Jest)
import { getUtcOffsetForLocation } from "../timezone";
describe("timezone", () => {
describe("getUtcOffsetForLocation", () => {
it("should work for Lisbon", () => {
expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]);
});
it("should work for Berlin", () => {
expect(getUtcOffsetForLocation("Berlin")).toEqual([1]);
});
it("should work for Germany", () => {
expect(getUtcOffsetForLocation("Germany")).toEqual([1]);
});
it("should work for the United States", () => {
// when the region has multiple timezones,
expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]);
});
it("should return null for a non-existing region", () => {
// when the region has multiple timezones,
expect(getUtcOffsetForLocation("Blablabla")).toEqual( null);
});
});
});
you can use toLocaleTimeString() to find out the time of a particular city of a aprticular country , For example i want to determine the current time in 24 hour of India , so run this script
`let indianTime = new Date().toLocaleTimeString("en-US",
{timeZone:'Asia/Kolkata',timestyle:'full',hourCycle:'h24'})
console.log(indianTime)`
similarly for time of Dhaka/Bangladesh we can do the same
`let bangladeshTime = new Date().toLocaleTimeString("en-US",
{timeZone:'Asia/Dhaka',timestyle:'full',hourCycle:'h24'})
console.log(bangladeshTime)`
here i used a parameter en-Us to get the standard time format
TimeZoneOffset :
var d = new Date()
alert(d.getTimezoneOffset());
toLocaleTimeString() : This converts time to the local.
var d = new Date();
alert(d.toLocaleTimeString());
Using a library: refer, Auto Time zone detection & momentjs
function getTimeOffset(country = 'America/New_York', summerTime = false) {
let date = new Date(new Date().getFullYear(), summerTime ? 6 : 11, 1);
let wordTime = new Date(date.toISOString().substr(0, 19)).getTime();
let localTime = new Date(date.toLocaleString('en', { timeZone: country })).getTime();
return (wordTime - localTime) / 1000 / 60;
}
There is no default method. Although, there are few ways you can do it easily using the same getTimeZoneOffSet method. One such tutorial is here. http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/
function calcTime(city, offset) {
// create Date object for current location
d = new Date();
// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));
// return time as a string
return "The local time in " + city + " is " + nd.toLocaleString();
}
Note that this function requires you to pass the difference from GMT manually. You can use your code behind to get that parameter.

Categories