3

Is there anyway to set the global/default timezone in JS on the browsers side?

E.g. if I have some date that are +1300 and the user is -0800 it converts the times. But I always want to show the times as +1300.

My application is already very large so I'm hoping I can just set it in one place, otherwise it will involve going though hundreds of lines of code to adjust anywhere a date is used.

Shiva
  • 20,575
  • 14
  • 82
  • 112
Petah
  • 45,477
  • 28
  • 157
  • 213

2 Answers2

4

No. There is only one global timezone: UTC - unfortunately it's not the default in JS, you have to use the …UTC… methods explicitly to get away from the user's local timezone.

You cannot set a timezone in JavaScript. Start with UTC and add/subtract hours if you want to display a custom timezone, like in this example.

it will involve going though hundreds of lines of code to adjust anywhere a date is used.

No. You only need to adjust the lines where a datetime is read or written. If the user does input dates according to his local timezone, you don't even need to fix that - only where you want to output it.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
-2

JavaScript has a built in method to retrieve the timezone offset of the current machine:

var d = new Data();
var offset = d.getTimezoneOffset();

In the above example, offset would be the minutes offset from UTC to Local Time. If you know your own offset, you can write a global method to take theirs and run a conversion.

Bic
  • 3,141
  • 18
  • 29
  • 1
    OP does not want to retrieve the user's local timezone, he wants to overrule it. – Bergi Feb 10 '14 at 01:27
  • @Bergi That is exactly what I suggested. He wants to overwrite the time to a specific timezone. In order to overwrite the time to a specific timezone, he needs to know the offset of that time... – Bic Feb 10 '14 at 01:32
  • He knows his timezone in which he wants to output the time. What would he need the user's local timezone for? – Bergi Feb 10 '14 at 01:34