【JS 计算出生年月】

// html部分
<el-date-picker 
   @blur="getAge" 
   value-format="yyyy-MM-dd" 
   v-model="archiveRenshi.birthday" 
   format="yyyy.MM"  
   type="date"
   placeholder="--- 请您选择出生年月 ---">
</el-date-picker>



// JS 部分
getAge (birthDate) {
// 如果传入的是对象,把这里改成1,这样就不再return,而是直接把年龄写入archiveRenshi字段
// 这里写数组 如果用户不点击的话 默认显示空的输入框
      let flag = []
// @blur事件会传入一个事件对象,而非简单的年月日时间字符串
      if (typeof (birthDate) === 'object') { 
        birthDate = birthDate.value
        flag = 1
      }
      // 出生时间 年份
      let birthDateTimeStamp = new Date(birthDate).getYear()
      // 当前时间 年份
      let nowTimeStamp = new Date().getYear()
      // 一年毫秒数(365 * 86400000 = 31536000000)
      let age = nowTimeStamp - birthDateTimeStamp
      if (flag) {
        this.archiveRenshi.age = age
      } else {
        return age
      }
    },

 },

猜你喜欢

转载自blog.csdn.net/Guanchong333/article/details/126527870