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


    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <button  onClick="btnDayCount_Click();">计算相差天数</button>  
    <script type="text/javascript" >  // language="JavaScript"
       function  btnDayCount_Click(){  
          // alert("计算相差天数");
           var day1 =  "2002-1-10"  ;
           var day2 =  "2002-10-10"  ;
           alert(DateDiff(day1,day2))  ;
       }  

       //计算天数差的函数,通用  
       function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2002-12-18格式  
           var  aDate,  oDate1,  oDate2,  iDays  ;
           aDate  =  sDate1.split("-") ;
          /*  for (var i = 0; i < aDate.length; i++) {
              alert(aDate[i]);
        } */
           oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0]) ;   //转换为12-18-2002格式  
           aDate  =  sDate2.split("-") ;
           oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0]) ;
           //Math.abs(num)求出绝对值
           //parseInt(num)解析字符串,返回一个整数
           iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)  ;  //把相差的毫秒数转换为天数  
           return  iDays;  
    }
    </script>
    </body>
    </html>

  • 相关阅读:
    hdu 4521 小明系列问题——小明序列(线段树 or DP)
    hdu 1115 Lifting the Stone
    hdu 5476 Explore Track of Point(2015上海网络赛)
    Codeforces 527C Glass Carving
    hdu 4414 Finding crosses
    LA 5135 Mining Your Own Business
    uva 11324 The Largest Clique
    hdu 4288 Coder
    PowerShell随笔3 ---别名
    PowerShell随笔2---初始命令
  • 原文地址:https://www.cnblogs.com/gwq369/p/5510994.html
Copyright © 2020-2023  润新知