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

5
  • 1
    Does this answer your question? How to initialize a JavaScript Date to a particular time zone Commented Jun 22, 2020 at 10:23
  • Date instances don't have a timezone, they are just an offset from 1970-01-01, they have no other information. Commented Jun 22, 2020 at 10:59
  • @RobG So I just find this 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? Commented Jun 22, 2020 at 11:36
  • 1
    @SMAKSS— new Date(gmt).toISOString() is valid. The issue with the answer you linked to is explained in this comment, 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. Commented Jun 22, 2020 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. Commented Jul 1, 2020 at 9:33

2 Answers 2

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()}`);

Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

Please up-vote good answers and select the best. Hopefully - mine! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.