227

Moment version: 2.0.0

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

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

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

3 Answers 3

350

I think you missed a key point in the documentation for .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.

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 
Sign up to request clarification or add additional context in comments.

4 Comments

I really appreciate the response, Matt. It clears up many things. Here's what I still can't get my head around: every representation of returned_endate that I know how to access is still at midnight, and not 2am. I need it to be 2am so that I can make a D3 chart that I'm working on (see edited question). Thanks again.
Make sure you call one of the display methods, such as .format() or .toDate() or .unix(). Just looking at the raw moment isn't going to work well. You might also want to do something like yourmoment.utc().format() to format it as utc instead of local time.
Yeah, forgetting that these moments are not immutable gets ya every once in a while. Nice!
Note to self - .add(2, 'mins') silently fails... you want .add(2, 'minutes').
60

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.

3 Comments

It should be 'minutes' not in 'mins'.
It should be 'minutes' not 'mins'. Or at least it can be 'm'. Please update your answer !
it's already 'minutes' in my answer and I already mentioned that we can use 'm' instead of 'minutes'(see the comments of answer). I think no need to update my answer.
15

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

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" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.