Hướng dẫn dùng glidedate JavaScript

Given dates in the format YYYY-MM-DD, you can use the Date constructor, subtract one from the other and divide by the number of milliseconds in one day.

The strings will be parsed as UTC, so there are no issues with daylight saving and ECMAScript has exactly 8.64e7 ms per day. Even if you need to round, daylight saving doesn't change the value by more than 1 hour (and in some places less) so rounding is fine. E.g.

var d0 = '2017-07-31';
var d1 = '2017-07-28';
var diffDays = (new Date(d0) - new Date(d1)) / 8.64e7;

console.log(diffDays);

It's generally recommended to avoid the built–in parser, however in this case it's likely OK. But for confidence, parse the strings manually, a library can help but it only requires a 2 line function.

If your dates are not created as above and are not set to midnight, you can get days between dates by setting them to midnight, subtracting and rounding, e.g.

var d0 = new Date(2017, 6, 31, 23, 15, 45); // 2017-07-31T23:15:25 local
var d1 = new Date(2017, 6, 28, 3, 15, 45);  // 2017-07-28T03:15:25 local

function diffDays(d0, d1) {
  var a = new Date(d0).setHours(0,0,0,0);
  var b = new Date(d1).setHours(0,0,0,0);
  return Math.round((a - b) / 8.64e7);
}

console.log(diffDays(d0, d1));