<p>"2019/4/1"至"2019/4/2" 相差 <span id="di1"></span>天</p><p>"2016/5/28"至"2016/6/2" 相差 <span id="di2"></span>天</p>
var DateDiff = function (sDate1, sDate2) { // sDate1 和 sDate2 是 2016-06-18 格式
  var oDate1 = new Date(sDate1);
  var oDate2 = new Date(sDate2);
  var iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); // 把相差的毫秒數轉換為天數
  return iDays;
};

var GetDateDiff1 = DateDiff("2019/4/1","2019/4/2"); // 轉換為天數 : 1
console.log(GetDateDiff1);

var GetDateDiff2 = DateDiff("2016/5/28","2016/6/2"); // 轉換為天數 : 5 
console.log(GetDateDiff2);

document.getElementById('di1').innerText = GetDateDiff1;
document.getElementById('di2').innerText = GetDateDiff2;

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.