javascript 如何使用 moment.js 在特定时区创建时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18448347/
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 11:58:07  来源:igfitidea点击:

How to create time in a specific time zone with moment.js

javascripttimezonemomentjs

提问by DaniOcean

I have this backend that sends me a pre formatted time in a set time zone, but without any information for the said time zone. The strings are like: "2013-08-26 16:55:00".

我有这个后端,它在设定的时区向我发送一个预先格式化的时间,但没有关于所述时区的任何信息。字符串类似于:“2013-08-26 16:55:00”。

I can create a new moment.js instance with this string:

我可以用这个字符串创建一个新的 moment.js 实例:

var time = moment("2013-08-26 16:55:00") //this creates time in my tz

but this will only create an instance in my own time zone.

但这只会在我自己的时区创建一个实例。

Moment.js have a plugin that can create instances of the object in specific time zones and it works great, but I can't say what time I want the object to point to.

Moment.js 有一个插件,可以在特定时区创建对象的实例,而且效果很好,但我不能说我希望对象指向什么时间。

If I'm in New York and I do this:

如果我在纽约并且我这样做:

var time = moment("2013-08-26 16:55:00").tz("America/Los_Angeles");

the resulting time will be 13:55 instead of 16:55 but in LA.

结果时间将是 13:55 而不是 16:55 但在洛杉矶。

What I want is to create an instance that will say 16:55, but in LA time.

我想要的是创建一个实例,它会说 16:55,但在洛杉矶时间。

The reason I'm asking is because I want to do this:

我问的原因是因为我想这样做:

var now = moment.tz("America/Los_Angeles");
var end = moment("2013-08-26 16:55:00"); //plus something to convert LA time

var timeLeft = end.diff(now, "minutes");

Is there a way to do that?

有没有办法做到这一点?

回答by Matt Johnson-Pint

In most cases, you can simply do this:

在大多数情况下,您可以简单地执行以下操作:

moment.tz("2013-08-26 16:55:00", "America/Los_Angeles")

If you require input other than ISO8601, then specify the format string as the second parameter, and the time zone as the third:

如果您需要 ISO8601 以外的输入,则将格式字符串指定为第二个参数,将时区指定为第三个:

moment.tz("8/26/2013 4:55 pm", "M/D/YYYY h:mm a", "America/Los_Angeles")

And if you need to use moment's "strict parsing" mode, then that goes in the third parameter, and the time zone moves to the fourth position:

如果你需要使用 moment 的“严格解析”模式,那么在第三个参数中,时区移动到第四个位置:

moment.tz("8/26/2013 4:55 pm", "M/D/YYYY h:mm a", true, "America/Los_Angeles")

回答by timmcliu

If you want to calculate everything in a specific timezone you want to set the default time zone using

如果要计算特定时区中的所有内容,则要使用设置默认时区

A) moment.tz.setDefault("America/Los_Angeles");

一个) moment.tz.setDefault("America/Los_Angeles");

For my use case (in a node.js project) I just set it right after requiring the moment modules like so:

对于我的用例(在 node.js 项目中),我只是在需要像这样的 moment 模块后立即设置它:

let moment = require('moment');
require('moment-timezone');
moment.tz.setDefault("America/Los_Angeles");

All calls to moment()thereafter will create the time in the "America/Los_Angeles" setting, which is NOT the same as using:

moment()此后的所有调用都将在“America/Los_Angeles”设置中创建时间,这与使用不同:

B) moment.tz("2017-03-04 00:00", "America/Los_Angeles")

乙) moment.tz("2017-03-04 00:00", "America/Los_Angeles")

OR

或者

C) moment("2017-03-04 00:00").tz("America/Los_Angeles")

C) moment("2017-03-04 00:00").tz("America/Los_Angeles")

both of which would create the moment object in UTC time (unless you already changed the default), and then convert it to be the Los Angeles timezone.

两者都将在 UTC 时间创建时刻对象(除非您已经更改了默认值),然后将其转换为洛杉矶时区。

Running B or C above in the browser console yields:

在浏览器控制台中运行上面的 B 或 C 会产生:

_d: Fri Mar 03 2017 16:00:00 GMT-0800 (PST)
_i: "2017-3-4 00:00"

Notice _d shows March 3 4:00pm; this is because the moment object is created with March 4 12:00am in UTC time, then converted to Pacific timezone, which is 8 hours behind/the previous day.

通知_d显示3月3日下午4点;这是因为 moment 对象是在 UTC 时间的 3 月 4 日凌晨 12:00 创建的,然后转换为太平洋时区,比前一天晚 8 小时。

source: http://momentjs.com/timezone/docs/#/using-timezones/default-timezone/

来源:http: //momentjs.com/timezone/docs/#/using-timezones/default-timezone/

回答by JOG

install moment-timezone

安装时刻时区

> npm install moment-timezone

> npm install moment-timezone

Or see https://momentjs.com/timezone/docs/

或查看https://momentjs.com/timezone/docs/

.tz(string, string)

.tz(字符串,字符串)

moment.tz("2020-01-02 13:33:37", "Iran/Tehran")

moment.tz("2020-01-02 13:33:37", "Iran/Tehran")