- 2 years ago 221
Javascript Question

Convert string to Timestamp in Javascript using MomentJS

I have a string like :

var from = '2016-06-06T21:03:55' ;

Now how do I convert it into a timestamp such that I can give it as an input to momentjs.
Basically, I want to find the difference in the timestamps as seen in this post : Get hours difference between two dates in Moment Js

Look at the last answer in the above post.

Please please help em out. I am stck on it since hours now.

Ash Ash
Answer Source

Pass this variable to the moment ctor.

var from = '2016-06-06T21:03:55' ;
var a = moment('2016-06-06T22:03:55');
var b = moment(from);

console.log(a.diff(b, 'minutes')); //60
console.log(a.diff(b, 'hours')); //1
console.log(a.diff(b, 'days')); //0 
console.log(a.diff(b, 'weeks')); //0

Or you can use the format method to see the value fo the moment variables, like :

b.format('DD/MM/YYYY hh:mm:ss'); // 06/06/2016 09:03:55
b.format(); // 2016-06-06T21:03:55+10:00
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download