js时间比较示例分享(日期比较)_技术学院_宜昌市隼壹珍商贸有限公司

您好,欢迎访问宜昌市隼壹珍商贸有限公司

400 890 5375
当前位置: 主页 > 新闻动态 > 技术学院

js时间比较示例分享(日期比较)

发布时间:2026-01-17  |  点击率:

复制代码 代码如下:
<html>
 <head>
  <script language="javascript" type="text/javascript">
   /** 日期比较 **/
   function compareDate(strDate1,strDate2)
   {
    var date1 = new Date(strDate1.replace(/\-/g, "\/"));
    var date2 = new Date(strDate2.replace(/\-/g, "\/"));
    return date1-date2;
   }

   /** 比较 **/
   function doCompare(){
    var strDate1 = document.getElementById("strDate1").value;
    var strDate2 = document.getElementById("strDate2").value;
    var result = compareDate(strDate1,strDate2);
    if ( result>0 ) {
     alert("strDate1晚于strDate2");
    }else if( result<0 ){
     alert("strDate1早于strDate2");
    }else if ( result==0 ){
     alert("strDate1等于strDate2");
    }
   }
  </script>
 </head>
 <body>
  <input type="text" id="strDate1" name="strDate1" value="2012-07-01"/>
  <input type="text" id="strDate2" name="strDate2" value="2012-08-01"/>
  <input type="button" id="compareBtn" name="compareBtn" value="比较" onClick="doCompare();"/>
 </body>
</html>

全国统一服务电话

400 890 5375

电子邮箱:879577@qq.com

公司地址:宜昌市西陵区黄河路5号三峡明珠10栋1051室

咨询微信

TEL:13680874598