Home  >  Article  >  Web Front-end  >  How to compare the number of days between two dates in javascript_javascript skills

How to compare the number of days between two dates in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:49:221565browse

The example in this article describes the method of comparing the number of days between two dates in javascript. Share it with everyone for your reference. The details are as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script language="JavaScript">
  function getDate(strDate){
    if(strDate==null||strDate===undefined) return null;
    var date = new Date();
    try{
      if(strDate == undefined){ 
        date= null;
      }else if(typeof strDate == 'string'){
        strDate = strDate.replace(/:/g,'-');
        strDate = strDate.replace(/ /g,'-');
        var dtArr = strDate.split("-");
        if(dtArr.length>=3&&dtArr.length<6){
          date=new Date(dtArr[0], dtArr[1], dtArr[2]);
        }else if(date.length>8){
          date=new Date(Date.UTC(dtArr[0],dtArr[1]-1,dtArr[2],dtArr[3]-8,dtArr[4],dtArr[5]));
        }
      }else{
        date = null;
      }
      return date;
    }catch(e){ 
      alert('格式化日期出现异常:' + e.message); 
    } 
  }
  function test(){
    var time1 = "2011-12-12";
    var time2 = "2011-12-10";
    var timeslong = getDate(time1).getTime()-getDate(time2).getTime();
    alert(timeslong/(1000*60*60*24))
  }
  test();
</script>
</head>
<body>
</body>
</html>

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn