根据身份证获取年龄和出生日期

queryage() {
            //得到年龄
            if (this.model.idCard && this.model.idCard.length == 18) {
                this.model.brthday = this.model.idCard.substring(6, 10) + '-' + this.model.idCard.substring(10, 12) + '-' + this.model.idCard.substring(12, 14);// 获取出生日期
                let age = this.getAge();
                this.$set(this.model,'age',age);
                this.$forceUpdate();
            } else {
                this.model.age = '';
            }
        },
        // 判断用户的年龄
        getAge() {
            let birthdays = new Date(this.model.brthday.replace(/-/g, '/'));
            let d = new Date();
            let age =
                d.getFullYear() -
                birthdays.getFullYear() -
                (d.getMonth() < birthdays.getMonth() || (d.getMonth() == birthdays.getMonth() && d.getDate() < birthdays.getDate()) ? 1 : 0);
            return age;
        },








watch: {
        // model: {
        //     handler(newName, oldName) {
        //         this.queryage();
        //     },
        //     deep: true,
        //     immediate: true
        // }
    },

 

 

 

posted @ 2021-05-10 15:10  Deer_Lin  阅读(248)  评论(0)    收藏  举报