根据身份证号 获取生日、年龄和性别

 //根据身份证获取年龄和性别
    function analyzeIDCard(obj) {
        //console.log('123')
        var IDCard = $(obj).val();
        var birth = IDCard.substring(6, 10) + "-" + IDCard.substring(10, 12) + "-" + IDCard.substring(12, 14);
        //console.log(birth);
        var sex = "";
        if (parseInt(IDCard.substr(16, 1)) % 2 == 1) {
            sex = "男";
        } else {
            sex = "女";
        }
        //console.log(sex);
        //获取年龄
        var myDate = new Date();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        var age = myDate.getFullYear() - IDCard.substring(6, 10) - 1;
        if (IDCard.substring(10, 12) < month || IDCard.substring(10, 12) == month && IDCard.substring(12, 14) <= day) {
            age++;
        }
        //console.log(age);
        $("#Age").val(age);

        if (sex == "男")
            $("input[name=Sex][value=true]").prop("checked", true);
        else
            $("input[name=Sex][value=false]").prop("checked", true);
    }

 

posted @ 2021-11-06 16:36  敲键盘的喵~  阅读(242)  评论(0编辑  收藏  举报