Search This Blog

Saturday 19 March 2016

working with DateTimes in nodejs/find difference between two datetimes

this is about playing with DateTimes in NodeJS. Javascript has good no.of datetime functions but to simplify it a bit more we can use moment npm package which offers a rich set of datetime functions.

to install it use  npm install moment --save

sample usage:

var StartTS = moment("Wed Mar 25 2015 01:00:00 GMT",'YYYY-M-DD HH:mm:ss');
var EndTS = moment("Wed Mar 26 2015 12:19:43 GMT",'YYYY-M-DD HH:mm:ss');
the above create dates like 2016-03-24 01:00:00 format.

to find difference between the above two use the below:
var secondsDiff = EndTS.diff(StartTS, 'seconds')

it returns the time difference in seconds. this is simple handy.


No comments:

Post a Comment