Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding minutes to datetime in momentjs

Tags:

I need to add the variable secondsToMinutes to the startdate.

secondsToMinutes is "3:20" startDate = "2:00 PM" endDate should equal "2:03:20 PM". I've tried a number of ways and get errors each and every time.

   var startdate = data.StartTime;    startdate = moment(startdate).format('LTS');     var secondsToMinutes = readableDuration(self.runlength());//='3:20';     var seconds = secondsToMinutes.split(':')[1];    var minutes = secondsToMinutes.split(':')[0];     var date = moment(startdate)         .add(seconds, 'seconds')         .add(minutes, 'minutes')         .format('LTS'); 

Date shows up as invalid date.

like image 544
CsharpBeginner Avatar asked Feb 03 '17 19:02

CsharpBeginner


People also ask

How do I add time to a moment date?

This is a pretty robust function for adding time to an existing moment. To add time, pass the key of what time you want to add, and the amount you want to add. moment(). add(7, 'days');

Is Momentjs deprecated?

Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.


2 Answers

moment().format("LTS") returns a string value in hh:mm:ss AM/PM format. When you create a moment object using a string that is not in standard format, you should pass the input format as second parameter to moment constructor.

For eg: Jan 1, 2017 in string 01012017 is not a standard representation. But if you need a moment object out of it, using moment("01012017") will give "Invalid Date" response when formatting. Instead, use moment("01012017","DDMMYYYY")

var d = moment("01012017") d.toISOString() => "Invalid date"  var d = moment("01012017", "DDMMYYYY") d.toISOString() => "2016-12-31T18:30:00.000Z" 

In your code, when creating 'date' variable pass "hh:mm:ss A" as second parameter in the moment constructor as mentioned below .

   var date = moment(startdate, "hh:mm:ss A")         .add(seconds, 'seconds')         .add(minutes, 'minutes')         .format('LTS'); 
like image 172
Muralidharan Avatar answered Oct 03 '22 03:10

Muralidharan


Moment has really good documentation. I would check it out: http://momentjs.com/docs/

But to address your question more directly, you could do something like:

var secondsToMinutes = '3:20'; var seconds = secondsToMinutes.split(':')[1]; var minutes = secondsToMinutes.split(':')[0];  var momentInTime = moment(...)                    .add(seconds,'seconds')                    .add(minutes,'minutes')                    .format('LT'); 

You should use the actual handlers to the best of your ability. There are some cool things you can do with durations now, but this is more succinct.

Edit:

As mentioned in a different answer:

 moment('2:00:00 PM', 'h:mm:ss A') 

Is necessary if you're handling that format. Regardless - adding/subtracting minutes/hours to a moment object is trivial. Passing invalid strings to a moment object is a different issue in-and-of itself. ;)

like image 24
The Dembinski Avatar answered Oct 03 '22 03:10

The Dembinski