计算年龄

computeAge(birthday) {
let getYeardata = birthday.split(" ")[0];
let getTimedata = birthday.split(" ")[1];
// 年月日 时分秒
const beforeYear = getYeardata.split("-")[0];
const beforeMonth = getYeardata.split("-")[1];
const beforeDate = getYeardata.split("-")[2];
const beforeHours = getTimedata.split(":")[0];
const beforeMinutes = getTimedata.split(":")[1];
const beforeSeconds = getTimedata.split(":")[2];
const nowDate = new Date();
nowDate.getFullYear(); //获取完整的年份(4位,1970-????)
nowDate.getMonth(); //获取当前月份(0-11,0代表1月)
nowDate.getDate(); //获取当前日(1-31)
nowDate.getHours(); //获取当前小时数(0-23)
nowDate.getMinutes(); //获取当前分钟数(0-59)
nowDate.getSeconds(); //获取当前秒数(0-59)
// 计算差值
let year = nowDate.getFullYear() - beforeYear;
let month = nowDate.getMonth() - beforeMonth + 1;
let day = nowDate.getDate() - beforeDate;
let hour = nowDate.getHours() - beforeHours;
let minute = nowDate.getMinutes() - beforeMinutes;

while (minute < 0 || hour < 0 || day < 0 || month < 0 || year < 0) {
if (minute < 0) {
--hour;
minute = 60 + minute;
} else if (hour < 0) {
--day;
hour = 24 + hour;
} else if (day < 0) {
--month;
day = 30 + day;
} else if (month < 0) {
--year;
month = 12 + month;
}
};
this.form.setFieldsValue({year: year});
this.form.setFieldsValue({month: month});
this.form.setFieldsValue({day: day});
this.form.setFieldsValue({time: hour});
},
posted @ 2021-02-08 17:05  你没有小JJ  阅读(75)  评论(0)    收藏  举报