輸入身份證顯示出生年月日.html
< script type ="text/javascript" > ...
//<![CDATA[
var IdFt = "445121820929367";
var IdEt = "445121198209293677";
var IdEt1 = "445121198209293677x";
document.write(fGetBirthDayById(IdFt, false, true), "<hr/>");
document.write(fGetBirthDayById(IdFt, true, true).toLocaleDateString(), "<hr/>");
document.write(fGetBirthDayById(IdEt, false, true), "<hr/>");
document.write(fGetBirthDayById(IdEt1, true, true).toLocaleDateString(), "<hr/>");
function fGetBirthDayById(Id, bReturnDateObj, bDebug)
...{// shawl.qiu code : return DateObj Or String
var Debug = bDebug;
if(Id.constructor == String||Id.constructor == Number)
...{
Id = [Id];
}
Id[0]+="";
Id[0] = Id[0].replace(/^(d...{18}|d...{15})(?:[sS]*)/, "$1");
var Y, M, D;
switch(Id[0].length)
...{
case 15:
Y = 19+Id[0].slice(6, 8);
M = Id[0].slice(8, 10);
D = Id[0].slice(10, 12);
break;
case 18:
Y = Id[0].slice(6, 10);
M = Id[0].slice(10, 12);
D = Id[0].slice(12, 14);
break;
default:
throw new Error("請輸入正确的身份證号碼!");
break;
}
if(bDebug)
...{
document.write("Id[0]: ", Id[0], "<br/>");
document.write("Id[0].length: ", Id[0].length, "<br/>");
document.write("Y: ", Y, "<br/>");
document.write("M: ", M, "<br/>");
document.write("D: ", D, "<br/>");
document.write("<p/>");
}
if(bReturnDateObj)
...{
M-=1;
return new Date(Y, M, D);
}
return [Y, "-", M, "-", D].join("");
} // end function fGetBirthDayById(Id, bReturnDateObj, bDebug)
//]]>
</ script >