Jean Dupont Jean Dupont - 3 months ago 6x
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


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