问题
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.
回答1:
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.
回答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.
来源:https://stackoverflow.com/questions/21667389/set-js-global-time-zone