js 日期相差的天数

喜夏-厌秋 提交于 2019-11-29 05:38:13

js 日期相差的天数

 

     /*js将yyyymmddhhmmss字符date计算相差天数*/
	  function getDifDays( s1,s2 ){
		var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
		var ms = ""
		var formatedS1 = s1.replace(pattern, '$1-$2-$3 $4:$5:$6');
		var formatedS2 = s2.replace(pattern, '$1-$2-$3 $4:$5:$6');
		
		s1 = new Date(formatedS1.replace(/-/g, '/'));
             s2 = new Date(formatedS2.replace(/-/g, '/'));
             ms = Math.abs(s1.getTime() - s2.getTime());
             return ms / 1000 / 60 / 60/24;
	}
       function getDifDays( s1,s2 ){
		var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
		var ms = ""
		
		s1 = new Date(s1.replace(pattern, '$1/$2/$3 $4:$5:$6'));
        s2 = new Date(s2.replace(pattern, '$1/$2/$3 $4:$5:$6'));
        ms = Math.abs(s1.getTime() - s2.getTime());
     
        return ms / 1000 / 60 / 60/24;
	}    

  

  

function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式    
          var  aDate,  oDate1,  oDate2,  iDays    
          aDate  =  sDate1.split("-")    
          oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2006格式    
          aDate  =  sDate2.split("-")    
          oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    
          iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数   
          return  iDays + 1   
      }
 
var day1 = (new Date).setFullYear(2011, 7, 16);
var day2 = (new Date).setFullYear(2011, 7, 20);
var number_of_days = (day2 - day1) / 86400000;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!