Javascript Set Timezone Offset and time settings on Client

Iman Mohamadi iman.mohamadi.dev at gmail.com
Sun Apr 29 16:49:03 UTC 2018


To clear things out, I don't mean any changes to the actual Date time of
Javascript. What I want to discuss about is a problem with a very simple
solution.

Issue: Since Javascript code is mainly executed on a device that you might
not have access to  timezone and time settings that would be very help full
to provide a way to adjust timezone and time settings that are taken from
OS via Javascript code. Since now if you set your OS timezone setting to
"Tehran" you will get -270 when you write this code in your browser
console:

```
(new Date()).getTimezoneOffset();
>> -270
```

This is cool but what If I know I want to use another timezone? With
current implementation I should mess around with date objects and do hacks
and tricks but a very easy way would be to allow me to override OS
preferences. That would be like so:

```
Date.setClientTimezoneOffset(300)    //Chicago timezone
(new Date()).getTimezoneOffset();
>> 300
```
Now I don't need to worry about myDate.getHours(), and minutes and seconds.
Right now Javascript keeps this timezone offset to itself and leave me in
pain of dealing with all incoming issues. However a simple set method could
save a lot of time. This is the same with time settings. My browser takes
time settings form my OS, if my OS time is 10:30 and I do this:

```
var myDate = new Date();
console.log(myDate.getHours());
>> 10
console.log(myDate.getMinutes());
>> 30
```
But what if correct time is 12:00 ? This can be annoying to fix how ever It
could be handled in browser like this:

```
Date.setClientHours(12, 0, 0);
var myDate = new Date();
console.log(myDate.getHours());
>> 12
console.log(myDate.getMinutes());
>> 0
```

This can be implemented by keeping 12:00 - 10:30 = 1:30h which is OS to
developer preference difference and use this offset to create correct
Date.now(). If user changes it's time settings it can be set back to OS
preferences.

What do you thing about the main idea?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180429/84bbdb4e/attachment.html>


More information about the es-discuss mailing list