Skip to content

Node.js based forum software built for the modern web

fix: user profile age off by a few days due to fixed-length year math (#14687)

Click&Go7d573372906f
diff --git a/src/controllers/accounts/helpers.js b/src/controllers/accounts/helpers.js
index b7ab301736..b51a8f175b 100644
--- a/src/controllers/accounts/helpers.js
+++ b/src/controllers/accounts/helpers.js
@@ -53,10 +53,7 @@ helpers.getUserDataByUserSlug = async function (userslug, callerUID, query = {})
delete userData.reputation;
}
- userData.age = Math.max(
- 0,
- userData.birthday ? Math.floor((new Date().getTime() - new Date(userData.birthday).getTime()) / 31536000000) : 0
- );
+ userData.age = Math.max(0, calculateAge(userData.birthday));
userData = await user.hidePrivateData(userData, callerUID);
userData.emailHidden = !userSettings.showemail;
@@ -348,6 +345,23 @@ async function getProfileMenu(uid, callerUID) {
return data;
}
+function calculateAge(birthday) {
+ if (!birthday) {
+ return 0;
+ }
+ const birthDate = new Date(birthday);
+ const today = new Date();
+ let age = today.getFullYear() - birthDate.getFullYear();
+ const hasHadBirthdayThisYear = (
+ today.getMonth() > birthDate.getMonth() ||
+ (today.getMonth() === birthDate.getMonth() && today.getDate() >= birthDate.getDate())
+ );
+ if (!hasHadBirthdayThisYear) {
+ age -= 1;
+ }
+ return age;
+}
+
async function parseAboutMe(userData) {
if (!userData.aboutme) {
userData.aboutme = '';