设置JS全局时区 [英] Set JS global time zone

查看:312
本文介绍了设置JS全局时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何在浏览器端设置JS的全局/默认时区吗?

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

例如。如果我的日期是 +1300 ,并且用户 -0800 则转换时间。但我总是希望将时间显示为 +1300

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.

推荐答案

不。只有一个全局时区:UTC - 不幸的是它不是JS中的默认时区,你必须使用 ... UTC ... 方法明确地远离用户的本地时区。

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.

您无法在JavaScript中设置时区。如果要显示自定义时区,请以UTC开头并添加/减去小时数,例如此示例

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.

这篇关于设置JS全局时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆