简体   繁体   English

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

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

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?有没有办法做到这一点?

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")

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"); A) 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") B) 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;通知_d显示3月3日下午4点; 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.这是因为 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/

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")

I had a similar issue for which i had to use New York based time so i had to consider for daylight savings.我有一个类似的问题,我不得不使用基于纽约的时间,所以我不得不考虑夏令时。 I tried using the above few answers but wasn't able to get it working.我尝试使用上述几个答案,但无法使其正常工作。 Then I solved my issue like the below code然后我像下面的代码一样解决了我的问题

import moment from 'moment-timezone'

const time = timestamp
const offset = moment.tz.zone('America/New_York')?.parse(time)
const date = moment(time).add(offset, 'minutes').toISOString()

or you can do this way which will consider the time offset on its own when you display locally.或者您可以这样做,当您在本地显示时,它会自行考虑时间偏移。

const time = moment.tz(timestamp, 'America/New_York')
const localtz = moment.tz.guess()
const date = time.clone().tz(localtz)

this gives you an ISO string which you can use as below这为您提供了一个 ISO 字符串,您可以使用如下

moment(date).local().format('dddd, MMM DD YYYY, hh:mm A')

or in whatever format you would like to display it或以您想要显示的任何格式

Just to make something abundantly clear, that is implied in other answers but not really stated:只是为了清楚地说明一些事情,其他答案中暗示了这一点,但并未真正说明:

  • You absolutely must either你绝对必须要么
    • use ISO8601 as your date format or使用 ISO8601 作为日期格式或
    • specify the format your string is in指定您的字符串的格式

.. when using the .tz(string datetime, [string format,] string zone) function, if you want moment to interpret the datetime argument you give to be in the zone you give. .. 使用.tz(string datetime, [string format,] string zone)函数时,如果您希望 moment 将您提供的datetime参数解释为在您提供的zone If you omit format , be sure to pass an ISO8601 formatted string如果省略format ,请确保传递一个 ISO8601 格式的字符串


For 2 days I went round in circles, because my API was delivering a time string like "03 Feb 2021 15:00" and sure, it parsed OK, but it always used the timezone from my local machine, then converted to the timezone I gave:两天来,我兜兜转转,因为我的 API 提供了一个时间字符串,例如"03 Feb 2021 15:00" ,当然,它解析得很好,但它总是使用我本地机器上的时区,然后转换为我的时区给:

//this always resulted in "2021-02-03 10:00 EST" if run on a machine in UTC
moment.tz("03 Feb 2021 15:00", "America/Indianapolis").format("YYYY-MM-DD HH:mm z")

This was massively confusing: the parsing was clearly working fine, because the date was right but the time was always wrong by however many hours there were between the machine and the given zone string这非常令人困惑:解析显然工作正常,因为日期是正确的,但时间总是错误的,无论机器和给定的区域字符串之间有多少小时

Switching to ISO format input worked:切换到 ISO 格式输入有效:

//this always resulted in "2021-02-03 15:00 EST" if run on a machine in UTC
moment.tz("2021-02-03 15:00", "America/Indianapolis").format("YYYY-MM-DD HH:mm z")

As did declaring:正如声明:

//this always resulted in "2021-02-03 15:00 EST" if run on a machine in UTC
moment.tz("03 Feb 2021 15:00", "DD MMM YYYY HH:mm", "America/Indianapolis").format("YYYY-MM-DD HH:mm z")

I hope this saves someone some time我希望这可以节省一些时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM