Javascript converts the date format and the javascript format
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> Date input </title>
<Script>
Window. onload = function (){
Var aLaydate = document. getElementsByClassName ("date ");
For (var I = 0; I <aLaydate. length; I ++)
{
ALaydate [I]. onchange = function (){
Var dateValue = this. value;
DateValue = dateValue. replace (/\. /G ,"-");
DateValue = dateValue. replace (/\./g ,"-");
If (dateValue. length = 8 ){
Var temp = dateValue. substring () + "-" + dateValue. substring () + "-" + dateValue. substring (6, 8 );
DateValue = temp;
Console. log (dateValue );
}
If (CheckDT (dateValue )){
This. value = dateValue;
}
Else
{
Alert ("Date input error ");
}
}
}
}
Function CheckDT (str)
{
Var r = str. match (/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d {}) $ /);
If (r = null)
{
Return false;
}
Else
{
Var d = new Date (r [1], r [3]-1, r [4]);
Return (d. getFullYear () = r [1] & (d. getMonth () + 1) = r [3] & d. getDate () = r [4]);
}
}
</Script>
</Head>
<Body>
<Input placeholder = "Enter the date" class = "date">
</Body>
</Html>
Input YYYY. MM. DD and YYYY. MM. DD, YYYYMMDD into YYYY-MM-DD
The CheckDT function is found in Du Niang.
Simple and practical functions. You can use them directly.