javascript Momentjs 抓取今天的日期和设置时间使其快进 24 小时

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

Momentjs Grabbing today's date and setting time causes it to fast-forward 24 hours

javascriptnode.jstimezonemomentjs

提问by Warner Onstine

I'm trying to time out email messages based on user preferences. My morning calculations are working correctly but it's the evening emails that are never getting sent because dates aren't behaving as expected.

我正在尝试根据用户首选项超时电子邮件。我早上的计算工作正常,但晚上的电子邮件永远不会发送,因为日期没有按预期运行。

First, here's the code I'm using to grab the time and perform adjustments based on user location, etc.

首先,这是我用来抓取时间并根据用户位置等进行调整的代码。

var time = moment();
var machineTZ = time.zone();
var userTZ = 420;
var diffTZ = userTZ - machineTZ;

var oneHour = moment(time).add('minutes', 60);

var morningRun = moment().startOf('day');
morningRun.hour(7).minute(0);
morningRun.add('minutes', diffTZ);

var eveningRun = moment().startOf('day');
eveningRun.hour(19).minute(30);
eveningRun.add('minutes', diffTZ);

I'm checking every hour to see if it's time to schedule another email to go out. Right now this is hard-coded, but when I begin to add user preferences they'll be able to select their local time they'd like things to go out at.

我每小时检查一次,看看是否该安排另一封电子邮件发送出去。现在这是硬编码的,但是当我开始添加用户首选项时,他们将能够选择他们想要出去的当地时间。

I've been debugging my values to troubleshoot. Here's output from a job that ran early morning (from the server's perspective):

我一直在调试我的值以进行故障排除。这是清晨运行的作业的输出(从服务器的角度来看):

lastRun:    2013-10-12T00:06:55.088Z (this one is being run at 1 am)
morningRun: 2013-10-11T14:00:00.000Z
eveningRun: 2013-10-12T02:30:00.000Z

The run numbers are as I would expect them to be. In two hours time I want the evening email to go out (7:30pm my time = 2:30am the following day server-time).

运行数字正如我所期望的那样。在两小时内,我希望晚上发送电子邮件(我的时间下午 7:30 = 第二天服务器时间凌晨 2:30)。

Looking again an hour later we see:

一个小时后再次查看,我们看到:

lastRun:    2013-10-12T01:06:58.267Z (this one is at 2 am)
morningRun: 2013-10-12T14:00:00.000Z
eveningRun: 2013-10-13T02:30:00.000Z <---- what?

All of a sudden my calculation for my evening has flipped over the date line, even though it's still 10/12 (not 10/13 yet). Because of this my check to see if I should schedule the email fails since it now thinks I need to send the email in 24 hours, not 30 minutes.

突然间,我晚上的计算已经翻过了日期变更线,即使它仍然是 10/12(还不是 10/13)。因此,我检查是否应该安排电子邮件失败,因为它现在认为我需要在 24 小时内发送电子邮件,而不是 30 分钟。

Been battling with this weird inconsistency for a while, I thought I had figured out why it was doing it and adjusted my calculations using the time zone stuff above but that didn't do the trick :(. This certainly seems like some sort of weird bug as I would expect this to be happening:

一直在与这种奇怪的不一致作斗争,我以为我已经弄清楚为什么要这样做并使用上面的时区内容调整了我的计算,但这并没有解决问题:(。这当然看起来有点奇怪错误,因为我希望这会发生:

//Today is 10/12
var eveningRun = moment().startOf('day'); //10/12/2013 - 00:00:00
eveningRun.hour(19).minute(30); //10/12/2013 - 19:30
eveningRun.add('minutes', diffTZ); //10/13/2013 - 2:30 am

This works until at some point it decides that "today" is actually 10/13 and sets the evening run is to take place on 10/14 instead. Help is greatly appreciated, or if this is a bug would love to know what I can do to work around this issue until it's resolved.

这一直有效,直到在某个时候它决定“今天”实际上是 10/13 并设置晚上运行是在 10/14 进行。非常感谢帮助,或者如果这是一个错误,我很想知道我可以做些什么来解决这个问题,直到它得到解决。

回答by Matt Johnson-Pint

There's no need to calculate machineTZ, diffTZor add any minutes. Just do this instead:

有没有必要来计算machineTZdiffTZ或者添加任何分钟。只需这样做:

moment().zone(userTZ).startOf('day').hour(7).minute(0)

But do keep in mind that a value such as 420is not a time zone, it's a time zone offset. It only tells you what the offset is for a particular point in time. Since you are applying it unilaterally, you may get incorrect results during daylight saving time.

但请记住,诸如此类的值420不是时区,而是时区偏移量。它只告诉您特定时间点的偏移量是多少。由于您是单方面应用它,因此在夏令时期间您可能会得到不正确的结果。

Instead, you should try using the moment-timezoneaddon, and do something like this instead:

相反,您应该尝试使用moment-timezone插件,并执行以下操作:

moment().tz("America/Los_Angeles").startOf('day').hour(7).minute(0)

See also the timezone tag wiki, in particular the sections titled "Time Zone != Offset" and "The IANA/Olson Time Zone Database".

另请参阅时区标签 wiki,特别是标题为“时区 != 偏移量”和“IANA/Olson 时区数据库”的部分。