Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default TimeZone of the browser window in javascript

Tags:

javascript

gwt

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!!!

like image 616
Arun Avatar asked Jul 11 '12 11:07

Arun


People also ask

How do I change my timezone on my browser?

Change your date & time preferencesSelect Settings . Scroll down and select Advanced. In the "Date and time" section: To manually choose your time zone, select Time zone.

What is default timezone in JavaScript?

Sometimes, we may want to initialize a JavaScript date to a particular time zone. The default time zone is UTC.

Does JavaScript default to UTC?

The JavaScript Date is always stored as UTC, and most of the native methods automatically localize the result.

How do I find my timezone in JavaScript?

JavaScript Date getTimezoneOffset() getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.


2 Answers

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.

like image 77
Bergi Avatar answered Nov 03 '22 01:11

Bergi


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/

like image 26
Alberto Rivera Avatar answered Nov 02 '22 23:11

Alberto Rivera