简体   繁体   English

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

[英]Add a duration to a moment (moment.js)

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值。

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 . 您可以通过克隆时刻来解决此问题, 如此处所述

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

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. 可能它可以帮助你。

For people having a startTime (like 12h:30:30) and a duration (value in minutes like 120), you can guess the endTime like 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"

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

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