I have a requirement to find the difference between 2 times(times in HH:MM:SS) format.
Need to get the difference in seconds.
var a = moment('21:04:59');
var b = moment('21:04:00');
a - b = 59 seconds
I have tried following Its worked for me with date time format, but not time filed alone.
var a = moment('2016-06-06T20:30:59');
var b = moment('2016-06-06T20:30:00');
console.log(a.diff(b, 'seconds')) // 59 seconds
You simply have to add the right format (
'HH:mm:ss'
) when parsing your string into moment object.Your code gives a Deprecation warning because you are trying to parse a string that is not in a known ISO 8601 format. As the documentation states:
Note that, since you are not providing info about the day part, moment creates an object with the current date, month, and year.
Here a working example:
Funny thing: I gave you a similar answer a little time ago
In core, moment.js doesn't have support for time-only data. Even if you provide time-only data, it will try to store "today" as date.
But still you can do that.
Legal Way
Tricky Alternative
you have to pass time like following in
moment
OUTPUT
console.log(a.diff(b, 'seconds'))
// 59 seconds