vue中根据生日(出生年月日)计算年龄

<template>
    <div class="role" :style="backgroundImg">
    
    </div>
</template>

<script>
  export default{
     data(){
      return {
         teacherBirthday:'',//出生年月日
         teacherAge:'',//年龄
        }
    },
    methods:{
//      判断用户的年龄
      getAge(){
        let birthdays = new Date(this.teacherBirthday.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);
        this.teacherAge = age;
      },
    }
   }
</script>

猜你喜欢

转载自blog.csdn.net/JEFF_luyiduan/article/details/94603428