|
@@ -98,7 +98,7 @@
|
98
|
98
|
<el-table-column label="年龄" align="center" key="age" prop="age" v-if="columns[1].visible" sortable
|
99
|
99
|
:sort-method="sortAge">
|
100
|
100
|
<template slot-scope="scope">
|
101
|
|
- {{ getAgeByIdCard(scope.row.idCard) }}
|
|
101
|
+ {{ String(getAgeByIdCard(scope.row.idCard)).slice(0,2) }}
|
102
|
102
|
</template>
|
103
|
103
|
</el-table-column>
|
104
|
104
|
<el-table-column label="性别" align="center" key="sex" prop="sex" v-if="columns[2].visible"
|
|
@@ -793,29 +793,30 @@ export default {
|
793
|
793
|
}
|
794
|
794
|
},
|
795
|
795
|
getAgeByIdCard(idCard) {
|
796
|
|
- const sexAndAge = {}
|
797
|
|
- //获取用户身份证号码
|
798
|
|
- const userCard = idCard
|
799
|
|
- //如果用户身份证号码为undefined则返回空
|
800
|
|
- if (!userCard) {
|
801
|
|
- return ''
|
|
796
|
+ const birthStr = idCard.substring(6, 14);
|
|
797
|
+ const birthYear = parseInt(birthStr.substring(0, 4), 10);
|
|
798
|
+ const birthMonth = parseInt(birthStr.substring(4, 6), 10) - 1; // 月份从0开始
|
|
799
|
+ const birthDay = parseInt(birthStr.substring(6, 8), 10);
|
|
800
|
+ // 创建日期对象
|
|
801
|
+ const birthDate = new Date(birthYear, birthMonth, birthDay);
|
|
802
|
+ const now = new Date();
|
|
803
|
+ // 基础年龄计算
|
|
804
|
+ let age = now.getFullYear() - birthDate.getFullYear();
|
|
805
|
+ // 判断是否已过生日
|
|
806
|
+ const monthDiff = now.getMonth() - birthDate.getMonth();
|
|
807
|
+ if (monthDiff < 0 || (monthDiff === 0 && now.getDate() < birthDate.getDate())) {
|
|
808
|
+ age--;
|
802
|
809
|
}
|
803
|
|
- // 获取出生日期
|
804
|
|
- const yearBirth = userCard.substring(6, 10)
|
805
|
|
- const monthBirth = userCard.substring(10, 12)
|
806
|
|
- const dayBirth = userCard.substring(12, 14)
|
807
|
|
- // 获取当前年月日并计算年龄
|
808
|
|
- const myDate = new Date()
|
809
|
|
- const monthNow = myDate.getMonth() + 1
|
810
|
|
- const dayNow = myDate.getDate()
|
811
|
|
- let age = myDate.getFullYear() - yearBirth
|
812
|
|
-
|
813
|
|
- if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
|
814
|
|
- age--
|
|
810
|
+ // 计算上次生日日期
|
|
811
|
+ let lastBirthday = new Date(now.getFullYear(), birthDate.getMonth(), birthDate.getDate());
|
|
812
|
+ if (now < lastBirthday) {
|
|
813
|
+ lastBirthday.setFullYear(lastBirthday.getFullYear() - 1);
|
815
|
814
|
}
|
816
|
|
- // 得到年龄
|
817
|
|
- sexAndAge.age = age
|
818
|
|
- return sexAndAge.age
|
|
815
|
+ // 计算时间比例
|
|
816
|
+ const nextBirthday = new Date(lastBirthday.getFullYear() + 1, lastBirthday.getMonth(), lastBirthday.getDate());
|
|
817
|
+ const daysInYear = (nextBirthday - lastBirthday) / (1000 * 60 * 60 * 24);
|
|
818
|
+ const daysPassed = (now - lastBirthday) / (1000 * 60 * 60 * 24);
|
|
819
|
+ return Number((age + (daysPassed / daysInYear)).toFixed(4));
|
819
|
820
|
},
|
820
|
821
|
formatArrayToString(str, splitStr) {
|
821
|
822
|
let newStr = "";
|