用js 计算出生年月

103 阅读1分钟

getAge (birthDate) { let flag = 0 // 如果传入的是对象,把这里改成1,这样就不再return,而是直接把年龄写入archiveRenshi字段 if (typeof (birthDate) === 'object') { // @blur事件会传入一个事件对象,而非简单的年月日时间字符串 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 } },