This article mainly introduces how to convert the date format in javascript, which is very simple and practical and can be used frequently in projects. We recommend this article to you.
The Code is as follows:
Date input
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
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.