• js 日期相差的天数


    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;
  • 相关阅读:
    Java Varargs 可变参数使用
    GitLab 如何删除 Forked from
    Git Clone 的时候遇到 Filename too long 错误
    Spring Boot 如何部署到 Linux 中的服务
    Gradle 如何打包 Spring Boot 如何不添加版本代码
    HDU 5878---预处理+二分查找
    HDU 5881--Tea 思维规律
    HDU 5879---cure
    (转)C/S 与 B/S 区别
    卡特兰数
  • 原文地址:https://www.cnblogs.com/guoziyi/p/7058297.html
Copyright © 2020-2023  润新知