Home > Web Front-end > JS Tutorial > Pure javascript to determine whether the query date is a valid date_javascript skills

Pure javascript to determine whether the query date is a valid date_javascript skills

WBOY
Release: 2016-05-16 15:43:16
Original
1915 people have browsed it

The following content is mainly introduced to you through js code. The code is relatively simple and contains comments. Any good suggestions are welcome.

As shown below, when the query condition contains a date, such as "2012-3-4", verify whether the entered date string is a valid date before querying

var snapshot_createTime_begin=$(selector+" input[name='createTime_begin']").val().trim();
      var snapshot_createTime_end=$(selector +" input[name='createTime_end']").val().trim();
      try{
        //判断开始时间是否为有效的日期
        if(snapshot_createTime_begin!=""&&new Date(snapshot_createTime_begin).getDate()!=snapshot_createTime_begin.match(/-\d{0,2}$/g)[0].replace(/-/g,"")){
          throw new Error();
        }
        //判断结束时间是否为有效的日期
        if(snapshot_createTime_end!=""&&new Date(snapshot_createTime_end).getDate()!=snapshot_createTime_end.match(/-\d{0,2}$/g)[0].replace(/-/g,"")){
          throw new Error();
        }
        if(Date.parse(snapshot_createTime_begin)>Date.parse(snapshot_createTime_end)){
          //alert("开始日期不应当超过结束日期!");
          alert("开始日期不应当超过结束日期!");
          return ;
        }
        $.extend(pageObj,{
          createTimeBegin:snapshot_createTime_begin,
          createTimeEnd:snapshot_createTime_end,
        });
        initPagination();
      }catch(e){
        //alert("请输入有效日期!")
        alert("请输入有效日期!");
      }
Copy after login

js determines whether the year, month and day is a valid date

function isdate(intYear,intMonth,intDay){ 
 if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay)) return false;   
 if(intMonth>12||intMonth<1) return false; 
 if ( intDay<1||intDay>31)return false; 
 if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30)) return false; 
 if(intMonth==2){ 
   if(intDay>29) return false;  
   if((((intYear%100==0)&&(intYear%400!=0))||(intYear%4!=0))&&(intDay>28))return false; 
  }
 return true; 
}
Copy after login

The above code is to verify the validity of the date. I hope it will be helpful to everyone.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template