Home > Web Front-end > JS Tutorial > Javascript implements date format conversion_javascript skills

Javascript implements date format conversion_javascript skills

WBOY
Release: 2016-05-16 16:26:22
Original
1060 people have browsed it

复制代码 代码如下:





日期输入
<script><br> window.onload = function(){<br>     var aLaydate = document.getElementsByClassName("date");<br>     for(var i = 0;i < aLaydate.length;i )<br />     {<br />         aLaydate[i].onchange = function(){<br />             var dateValue = this.value;<br />             dateValue = dateValue.replace(/。/g,"-");<br />             dateValue = dateValue.replace(/./g,"-");<br />             if(dateValue.length == 8){<br />                 var temp = dateValue.substring(0,4) "-" dateValue.substring(4,6) "-" dateValue.substring(6,8);<br />                 dateValue = temp;<br />                 console.log(dateValue);<br />             }<br />             if(CheckDT(dateValue)){<br />                 this.value = dateValue;<br />             }<br />             else<br />             {<br />                 alert("日期输入错误");<br />             }<br />         }<br />     }<br /> }<br />  <br /> function CheckDT(str)    <br /> {    <br />     var r = str.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2})$/);    <br />     if(r==null)<br />     {<br />         return false;    <br />     }<br />     else<br />     {<br />         var d= new Date(r[1], r[3]-1, r[4]);    <br />         return (d.getFullYear()==r[1]&&(d.getMonth() 1)==r[3]&&d.getDate()==r[4]);<br />     }   <br /> }<br /> </script>





把输入的YYYY.MM.DD、YYYY。MM。DD、YYYYMMDD转为YYYY-MM-DD

CheckDT这个function是在度娘里找的。

很简单实用的功能吧,小伙伴们可以直接拿去使用。

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