Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  166] [ 4]  / answers: 1 / hits: 127177  / 7 Years ago, fri, february 3, 2017, 12:00:00

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.


More From » datetime

 Answers
48

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

[#59081] Wednesday, February 1, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shaynelandenb

Total Points: 293
Total Questions: 97
Total Answers: 94

Location: Monaco
Member since Fri, Sep 24, 2021
3 Years ago
;