Reputation: 410
Could there be a way I can change my local timezone to a different timezone whenever I want to create a new date.
For example:
const date = new Date().timezone("GMT-04:00");
or
const date = new Date("GMT-04:00");
My local timezone is GMT+0:300
but I want that whenever I want to create a new Date, it's default timezone is GMT-04:00
Upvotes: 0
Views: 166
Reputation: 10510
Let's say you want to get the GMT time in none ISO format, so all you have to do is to toLocaleString
and then specify the desired time zone.
So for GMT, it will look like this:
const gmt = new Date().toLocaleString("en-GB", {
timeZone: "GMT"
});
console.log(`GMT time: ${gmt}`);
But if you looking for ISO format you should create a new date from your previous one then use toISOString
for converting the format. For converting to ISO there is no need to pass locales argument to the date parser. Otherwise, the result will be wrong.
const gmt = new Date().toLocaleString({
timeZone: "GMT"
});
console.log(`GMT time in ISO: ${(new Date(gmt)).toISOString()}`);
Upvotes: 1
Reputation: 2804
Probably not what you're looking for, but here's how to calculate the difference:
dd=new Date();
console.log(dd);
dd.setMinutes(dd.getMinutes()+dd.getTimezoneOffset()-4*60);
console.log(dd);
Output:
Mon Jun 22 2020 13:23:33 GMT+0300 (Israel Daylight Time)
Mon Jun 22 2020 06:23:33 GMT+0300 (Israel Daylight Time)
Upvotes: 0