Arun
Arun

Reputation: 125

Set default TimeZone of the browser window in javascript

We are displaying schedules on our webpage which is build on GWT. Client system using different timezone from server and because of that, all the schedules were displaying wrong. Is there a way to set default time zone when we load the page? Like the way we do it in java:

TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));

Thanks!!!

Upvotes: 7

Views: 24655

Answers (2)

Alberto Rivera
Alberto Rivera

Reputation: 3752

In case you are using moment.js for your dates, you can set the default timezone for all newly created moments with:

moment.tz.setDefault(String)

https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/

Upvotes: 2

Bergi
Bergi

Reputation: 664648

No, you can't set the timezone of Date objects in javascript. Usually you use only UTC and epoch-based timestamps.

Only when creating a Date from a string or from year, month etc. the local timezone will be used, you can only get the timezone offset.

Converting a timezone can only be done by re-setting the Hours of the Date object (example described here), creating a date which looks-like having an offset timezone but is just utc.

Upvotes: 9

Related Questions