javascript计算两个日期相差的天数

快乐打工仔 分类:实例代码

在实际应用中可能需要计算书两个日期之间它们相差的天数。

代码实例如下:

//+--------------------------------------------------- 
//| 求两个时间的天数差 日期格式为 YYYY-MM-dd  
//+--------------------------------------------------- 
function daysBetween(DateOne,DateTwo){  
  var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-')); 
  var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1); 
  var OneYear = DateOne.substring(0,DateOne.indexOf ('-')); 
   
  var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-')); 
  var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1); 
  var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-')); 
   
  var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);  
  return Math.abs(cha); 
} 
console.log(daysBetween("2014-10-5","2015-11-5"));
网站出售中,有意者加微信:javadudu

回复

我来回复
  • 暂无回复内容