javascript 为片刻添加持续时间 (moment.js)

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

Add a duration to a moment (moment.js)

javascriptmomentjs

提问by Paul H

Moment version: 2.0.0

时刻版本:2.0.0

After reading the docs, I thought this would be straight-forward (Chrome console):

阅读文档后,我认为这很简单(Chrome 控制台):

var timestring1 = "2013-05-09T00:00:00Z";
var timestring2 = "2013-05-09T02:00:00Z";
var startdate = moment(timestring1);
var expected_enddate = moment(timestring2);
var returned_endate = startdate.add(moment.duration(2, 'hours'));
returned_endate == expected_enddate  // false
returned_endate  // Moment {_i: "2013-05-09T00:00:00Z", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array[7]…}

This is a trivial example, but I can't even get it to work. I feel like I'm missing something big here, but I really don't get it. Even this this doesn't seem to work:

这是一个微不足道的例子,但我什至无法让它工作。我觉得我在这里错过了一些重要的东西,但我真的不明白。即使这样,这似乎也不起作用:

startdate.add(2, 'hours')
    // Moment {_i: "2013-05-09T00:00:00Z", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array[7]…}

Any help would be much appreciated.

任何帮助将非常感激。

Edit:My end goal is to make an binary status chart like the one I'm working on here: http://bl.ocks.org/phobson/5872894

编辑:我的最终目标是制作一个二进制状态图表,就像我在这里工作的那样:http: //bl.ocks.org/phobson/5872894

As you can see, I'm currently using dummy x-values while I work through this issue.

如您所见,我在解决此问题时目前正在使用虚拟 x 值。

回答by Matt Johnson-Pint

I think you missed a key point in the documentation for .add()

我认为您错过了文档中的一个关键点 .add()

Mutates the original moment by adding time.

通过添加时间来改变原始时刻。

You appear to be treating it as a function that returns the immutable result. Easy mistake to make. :)

您似乎将其视为返回不可变结果的函数。容易犯错。:)

If you use the return value, it is the same actual object as the one you started with. It's just returned as a convenience for method chaining.

如果您使用返回值,则它与您开始使用的实际对象相同。它只是为了方便方法链接而返回。

You can work around this behavior by cloning the moment, as described here.

您可以通过克隆 moment 来解决此行为,如here所述

Also, you cannot just use ==to test. You could format each moment to the same output and compare those, or you could just use the .isSame()method.

此外,您不能仅用于==测试。您可以将每个时刻格式化为相同的输出并进行比较,或者您可以只使用该.isSame()方法。

Your code is now:

您的代码现在是:

var timestring1 = "2013-05-09T00:00:00Z";
var timestring2 = "2013-05-09T02:00:00Z";
var startdate = moment(timestring1);
var expected_enddate = moment(timestring2);
var returned_endate = moment(startdate).add(2, 'hours');  // see the cloning?
returned_endate.isSame(expected_enddate)  // true

回答by Mahima Agrawal

I am working on an application in which we track live route. Passenger wants to show current position of driver and the expected arrival time to reach at his/her location. So I need to add some duration into current time.

我正在开发一个跟踪实时路线的应用程序。乘客想要显示司机的当前位置和到达他/她的位置的预计到达时间。所以我需要在当前时间中添加一些持续时间。

So I found the below mentioned way to do the same. We can add any duration(hour,minutes and seconds) in our current time by moment:

所以我找到了下面提到的方法来做同样的事情。我们可以在当前时间中按时刻添加任何持续时间(小时、分钟和秒):

var travelTime = moment().add(642, 'seconds').format('hh:mm A');// it will add 642 seconds in the current time and will give time in 03:35 PM format

var travelTime = moment().add(11, 'minutes').format('hh:mm A');// it will add 11 mins in the current time and will give time in 03:35 PM format; can use m or minutes 

var travelTime = moment().add(2, 'hours').format('hh:mm A');// it will add 2 hours in the current time and will give time in 03:35 PM format

It fulfills my requirement. May be it can help you.

它满足我的要求。也许它可以帮助你。

回答by Made in Moon

For people having a startTime(like 12h:30:30) and a duration(value in minutes like 120), you can guess the endTimelike so:

对于有startTime(如 12h:30:30) 和duration(以分钟为单位的值,如 120) 的人,您可以猜测endTime如下:

const startTime = '12:30:00';
const durationInMinutes = '120';

const endTime = moment(startTime, 'HH:mm:ss').add(durationInMinutes, 'minutes').format('HH:mm');

// endTime is equal to "14:30"