0

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

Fahd Jamy
  • 365
  • 6
  • 7
  • 1
    Does this answer your question? [How to initialize a JavaScript Date to a particular time zone](https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone) – Alexandre Elshobokshy Jun 22 '20 at 10:23
  • Date instances don't have a timezone, they are just an offset from 1970-01-01, they have no other information. – RobG Jun 22 '20 at 10:59
  • @RobG So I just find [this](https://stackoverflow.com/a/54127122/11908502) post in SO, where the answer is similar and I just come up with it months ago, I just carefully read the comments, and found out, `(new Date(gmt)).toISOString()` is not actually a valid thing. Is it true? Is there any alternate for getting ISO format? – SMAKSS Jun 22 '20 at 11:36
  • 1
    @SMAKSS— `new Date(gmt).toISOString()` is valid. The issue with the answer you linked to is explained in [*this comment*](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript/54127122#comment98342821_54127122), i.e. the built–in parser is not required to correctly parse the result of *toLocaleString*. There is also no standard for how to treat date and times in the DST changeover period and browsers treat it differently. – RobG Jun 22 '20 at 20:21
  • Please keep in mind, if one of the answers works for you, please mark them as the answer to help other peeps in the community to find their solution easier if they facing the same issue. You can do this by using grey marks (tick) beside answers (you can only choose one), for more information please read [this](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). – SMAKSS Jul 01 '20 at 09:33

2 Answers2

1

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()}`);
SMAKSS
  • 9,606
  • 3
  • 19
  • 34
0

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)
iAmOren
  • 2,760
  • 2
  • 11
  • 23