Adding minutes to datetime in momentjs
Asked Answered
T

5

26

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.

Theresiatheresina answered 3/2, 2017 at 19:45 Comment(0)
T
39

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');
Tramontane answered 4/2, 2017 at 14:59 Comment(0)
C
10

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. ;)

Champaigne answered 3/2, 2017 at 20:5 Comment(2)
The "3:20" time can be parsed directly using moment.duration("3:20")Deneb
I edited my code to reflect your suggestion to the best of my knowledge. Im getting error on Date sayign invalid dateTheresiatheresina
I
8

In the last moment() call you're trying to parse the value '2:00:00 PM'. According to the docs you have to use String+Format call in this case:

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

When you do:

var date = moment('2:00:00 PM')
    .add(30, 'seconds')
    .add(2, 'minutes')
    .format('LTS');

The date will be an Invalid date. But if you pass format h:mm:ss A as a second parameter:

var date = moment('2:00:00 PM', 'h:mm:ss A')
    .add(30, 'seconds')
    .add(2, 'minutes')
    .format('LTS');

... the result is 2:02:30 PM.

Indication answered 3/2, 2017 at 22:34 Comment(0)
F
0

.add operation is deprecated from moment.js You can use dayjs to do same functionality.

dayjs.add(1, 'day')

If looking for more reasons to shift from moment: https://inventi.studio/en/blog/why-you-shouldnt-use-moment-js

Fulfil answered 11/3, 2021 at 19:3 Comment(0)
B
0

I was using a different format, but this worked:

// This was the initial time:

let startTime = moment().format('HH:mm:ss');

// I needed to use the initial time, and then add seconds(it could be done to minutes or hours) to it before re-using it:

startTime = moment(startTime ,'HH:mm:ss').add(10,'seconds').format('HH:mm:ss');
Bettor answered 24/6, 2022 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.