javascript 从2000年以后出生的人的身份证号码生成出生日期

rslzwgfq  于 8个月前  发布在  Java
关注(0)|答案(2)|浏览(77)

我正在寻找一个函数来生成/填充2000年以后出生的人的身份证号码的出生日期。
这是我现在的代码,但它不会扩展到2000年之后

var Year = idNumber.substring(0, 2);
   var Month = idNumber.substring(2, 4)
   var Day = idNumber.substring(4, 6)
   var dob = '19' + Year + '/' + Month + '/' + Day;

字符串
我如何修改它以适应> 2000?

oxalkeyp

oxalkeyp1#

2位数的年份处理是选择一个截止日期。你可以使用今年的日期+1(19)的出生日期,但你总是会得到一个问题,与人超过99岁。

function getDob(idNumber) {
   var Year = idNumber.substring(0, 2);
   var Month = idNumber.substring(2, 4)
   var Day = idNumber.substring(4, 6)
   
   var cutoff = (new Date()).getFullYear() - 2000
   
   var dob = (Year > cutoff ? '19' : '20') + Year + '/' + Month + '/' + Day;
   
   return dob;
}

console.log(getDob("050505"));
console.log(getDob("180227"));
console.log(getDob("891231"));
console.log(getDob("000101"));

字符串

qoefvg9y

qoefvg9y2#

class NationalData{
    BirthDate:Date=new Date()
    Day:number=0
    Month:number=0
    Year:number=0
    ErrorDate:boolean=false
    constructor(nationalNo:string){
        var c = nationalNo.substr(0,1)=='2'?'19':'20',
            y=c+nationalNo.substr(1,2),
            m=nationalNo.substr(3,2),
            d=nationalNo.substr(5,2);
        this.Day=Number(d)
        this.Month=Number(m)
        this.Year=Number(y)
        this.BirthDate=new Date(this.Year,this.Month,this.Day)
        this.ErrorDate=((this.Month>12 || this.Month<1) 
        || (this.Year%4==0 && this.Month==2 && this.Day>29 || this.Year%4!=0 && this.Month==2 && this.Day>28) 
        || ((this.Month==1 || this.Month==3 || this.Month==5 || this.Month==7 || this.Month==8 || this.Month==10 || this.Month==12) && this.Day>31)
        || ((this.Month==4 || this.Month==5 || this.Month==9 || this.Month==11) && this.Day>30)
        || (this.Day<1))
     
    }
}

字符串

相关问题