|
@@ -29,13 +29,11 @@ export const getTimeStamp = (str) => {
|
|
|
export const timestampToAge = (timestamp) => {
|
|
|
if (!timestamp) return null
|
|
|
const birthDate = new Date(timestamp);
|
|
|
- const currentDate = new Date();
|
|
|
- let age = currentDate.getFullYear() - birthDate.getFullYear();
|
|
|
- const monthDiff = currentDate.getMonth() - birthDate.getMonth();
|
|
|
+ const today = new Date();
|
|
|
+ let age = today.getFullYear() - birthDate.getFullYear();
|
|
|
+ const monthDiff = today.getMonth() - birthDate.getMonth();
|
|
|
// 如果当前月份小于出生月份,或者月份相同但日期未到生日
|
|
|
- if (monthDiff < 0 || (monthDiff === 0 && currentDate.getDate() < birthDate.getDate())) {
|
|
|
- age--;
|
|
|
- }
|
|
|
+ if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) age--;
|
|
|
return age;
|
|
|
}
|
|
|
|