最新消息:网站改版咯

js通过身份证号码获取生日性别

ECMAScript Yovae 1604浏览

最近开发需要用到的功能,javascript实现通过身份证提取生日和性别的实现代码,由于一代身份证的最后一位数为偶数为女性奇数为男性,二代身份证也是一样不过标志位是倒数第二位:

function getBirthById(value) {
            if (!value) {
                return "";
            }
            var year = "1900";
            var month = "1";
            var day = "1";
            if (value.length == 15) {
                year = "19" + value.substr(6, 2);
                month = value.substr(8, 2);
                day = value.substr(10, 2);
            } else if (value.length == 18) {
                year = value.substr(6, 4);
                month = value.substr(10, 2);
                day = value.substr(12, 2);
            } else {
                return "";
            }
            newDate = new Date(year, month - 1, day);
            if (newDate.toString() == "NaN") {
                return "";
            }
            else {
                return year + "-" + month + "-" + day;
            }
        }
function getSexById(value) {
            if (!value) {
                return "未知";
            } else if (value.length == 15) {
                return parseInt(value.substr(14, 1))%2?"男":"女";
            } else if (value.length == 18) {
                return parseInt(value.substr(16, 1))%2?"男":"女";
            } else {
               return "未知";
            }
       }

转载请注明:Yovae Studio » js通过身份证号码获取生日性别