由身份证号获取出生日期、年龄、性别

最新更新: 2019-07-11 17:46:25 阅读: 70次

根据身份证得到出生日期、年龄、性别信息

 function idcard_get_more (num){
            var len = num.length;
            var _return=[];
            if(len == 18 || len == 15){
                if(len == 18){
                    var sex = num.substr(16,1);
                    var bir = num.substr(6,8);
                }else if(len == 15){
                    var sex = num.substr(14,1);
                    var bir = num.substr(6,6);
                    bir = '19'+bir;
                }
                if(sex%2 == 1){ //识别性别
                    _return['sex']='男';
                }else{
                    _return['sex']='男';
                }
                //判断年龄是否符合规范
                var preg=/^\d{4}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))$/i;
                if(!preg.test(bir)) {
                    return false;
                }
                var year = bir.substr(0,4);
                var month = bir.substr(4,2); //月份从0算起
                var date = bir.substr(6,2);
                var time = new Date(year,month-1,date);
                _return['birthday']=year+'-'+month+'-'+date;
                var now = new Date();
                var age = Math.floor((now-time)/(1000*365.23*24*60*60));
                _return['age']=age;
                return _return;
            }
            return false;
        }