|
|
@@ -387,4 +387,38 @@ module.exports = {
|
|
|
console.log('tabbar 更新完成');
|
|
|
},100);
|
|
|
},
|
|
|
+ // 核心年龄计算函数
|
|
|
+ calculateAgeFromBirthday:function(birthday) {
|
|
|
+ const birthDate = new Date(birthday);
|
|
|
+ const currentDate = new Date();
|
|
|
+
|
|
|
+ // 验证日期有效性
|
|
|
+ if (isNaN(birthDate.getTime())) {
|
|
|
+ throw new Error('无效的生日日期');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (birthDate > currentDate) {
|
|
|
+ throw new Error('生日日期不能晚于当前日期');
|
|
|
+ }
|
|
|
+
|
|
|
+ let years = currentDate.getFullYear() - birthDate.getFullYear();
|
|
|
+ let months = currentDate.getMonth() - birthDate.getMonth();
|
|
|
+ let days = currentDate.getDate() - birthDate.getDate();
|
|
|
+
|
|
|
+ // 处理天数负数情况
|
|
|
+ if (days < 0) {
|
|
|
+ months--;
|
|
|
+ // 获取上个月的天数
|
|
|
+ const lastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0);
|
|
|
+ days += lastMonth.getDate();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理月份负数情况
|
|
|
+ if (months < 0) {
|
|
|
+ years--;
|
|
|
+ months += 12;
|
|
|
+ }
|
|
|
+
|
|
|
+ return years;
|
|
|
+ }
|
|
|
};
|