javascript 设置全球时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17697696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:15:28  来源:igfitidea点击:

Set global time zone

javascriptdatetimetimezonemomentjs

提问by Fabrizio Fortino

I am using Moment.js to handle dates in my web application. The server returns all the dates in milliseconds UTC. Now, I have to display the dates applying a specific timezone (based on the user settings).

我正在使用 Moment.js 来处理我的 Web 应用程序中的日期。服务器以毫秒为单位返回所有日期 UTC。现在,我必须显示应用特定时区的日期(基于用户设置)。

Is there any way to set the timezone globally instead of changing all the calls to momentjs to handle it?

有没有办法全局设置时区而不是更改所有对 momentjs 的调用来处理它?

回答by Der Hochstapler

You can set the default timezone in Momentby using:

您可以使用以下命令在 Moment 中设置默认时区

moment.tz.setDefault(String);

For example

例如

moment.tz.setDefault("America/New_York");

回答by guidos

Use the moment-timezone library found on the same website: http://momentjs.com/timezone/It let's you do things like:

使用在同一网站上找到的 moment-timezone 库:http: //momentjs.com/timezone/它可以让您执行以下操作:

moment(utcDateTime).tz(settings.timezone).format(settings.dateFormat);

I recommend implementing a user class/object that has a service function for translating UTC to the user's timezone. Have a look at this fiddle:

我建议实现一个用户类/对象,它具有将 UTC 转换为用户时区的服务功能。看看这个小提琴:

http://jsfiddle.net/guidosch/ofd4unhu/4/

http://jsfiddle.net/guidosch/ofd4unhu/4/

The dates are served as UTC, but the view uses the date formatting method of the user class to adjust and format the UTC date according to the user's preferences.

日期作为 UTC 提供,但视图使用用户类的日期格式化方法根据用户的偏好调整和格式化 UTC 日期。

回答by Nel

  1. npm install moment-timezone
  2. var moment = require('moment-timezone');and use this object instead of usual moment.
  3. moment.tz.setDefault(String);where String is a time zone identifier.
  1. npm install moment-timezone
  2. var moment = require('moment-timezone');并使用这个对象而不是通常的moment
  3. moment.tz.setDefault(String);其中 String 是时区标识符

For example:

例如:

var moment = require('moment-timezone');
moment.tz.setDefault("America/New_York");

Docs: https://momentjs.com/timezone/docs/

文档:https: //momentjs.com/timezone/docs/

回答by Scott Fanetti

Having run into this problem in the past, my solution was to create a moment factory that provisioned moment objects rom a base configuration. You can make it as transparent as requiring moment - referencing your package and using the class just like moment - but in reality you are calling a moment wrapper object that provisions the moment implementations with the selected timeZone.

过去遇到过这个问题,我的解决方案是创建一个时刻工厂,从基本配置中提供时刻对象。你可以让它像需要 moment 一样透明 - 引用你的包并像 moment 一样使用类 - 但实际上你正在调用一个 moment 包装器对象,该对象使用选定的 timeZone 提供 moment 实现。

回答by stephenr85

I've not done extensive testing, but it looks right on cursory tests. I was able to do this with Moment Timezone 0.0.1:

我没有做过广泛的测试,但在粗略的测试中看起来是正确的。我能够使用 Moment Timezone 0.0.1 做到这一点:

var serverTimezoneOffset = <?php echo timezone_offset_get(new DateTimeZone(date_default_timezone_get()), new DateTime('now')) / -60; ?>;
moment.updateOffset(new Date().getTimezoneOffset()-serverTimezoneOffset);