xinelu-applet-ui/api/conversion.js

31 lines
927 B
JavaScript
Raw Normal View History

2023-10-10 14:01:25 +08:00
export function getSex(idCard) {
2023-10-12 09:47:40 +08:00
if (idCard.length === 15) {
return ['女', '男'][idCard.substr(14, 1) % 2]
} else if (idCard.length === 18) {
return ['女', '男'][idCard.substr(16, 1) % 2]
}
return ''
2023-10-10 14:01:25 +08:00
}
export function getBirthday(idCard) {
2023-10-12 09:47:40 +08:00
var birthday = "";
if (idCard != null && idCard != "") {
if (idCard.length == 15) {
birthday = "19" + idCard.substr(6, 6);
} else if (idCard.length == 18) {
birthday = idCard.substr(6, 8);
}
birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-')
}
return birthday;
2023-10-10 14:01:25 +08:00
};
// 出生日期转年龄
export function getAgeFun(value) {
2023-10-12 09:47:40 +08:00
var birthdays = new Date(value.replace(/-/g, "/")); //value 是传入的值
var time = new Date(); //当前时间
var age = time.getFullYear() - birthdays.getFullYear() - (time.getMonth() < birthdays.getMonth() || (time
.getMonth() == birthdays.getMonth() &&
time.getDate() < birthdays.getDate()) ? 1 : 0);
return age;
2023-10-10 14:01:25 +08:00
}