Jean Dupont Jean Dupont - 1 year ago 83
Javascript Question

How can I calculate difference between two dates with time in minutes

I'm working with javascript and I have dates in this format "2016-04-15 1230" (yyyy-MM-dd HHmm) and I want to calculate the difference between these dates in minutes.

Example :

The difference between "2016-04-15 1230" and "2016-04-15 1210" is 20 minutes

Answer Source

moment.js is a good library for this sort of thing:

var firstTime = moment("2016-04-15 1230", "YYYY-MM-DD HHmm");
var secondTime = moment("2016-04-15 1210", "YYYY-MM-DD HHmm");

var difference = moment.duration(firstTime - secondTime);

console.log(difference.asMinutes()); // 20
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download