function calculateAge(birthdate) {
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const birthYear = birthdate.getFullYear();
if (birthYear >= currentYear) {
console.log('birthYear >= currentYear');
return 0;
}
const yDiff = currentYear - birthYear;
const currentMonth = currentDate.getMonth();
const birthMonth = birthdate.getMonth();
console.log({
birthdate,
currentDate,
currentYear,
birthYear,
yDiff,
currentMonth,
birthMonth,
});
if (birthMonth > currentMonth) {
console.log('birthMonth > currentMonth');
return yDiff - 1;
}
if (birthMonth < currentMonth) {
console.log('birthMonth < currentMonth');
return yDiff;
}
const currentDay = currentDate.getDay();
const birthDay = currentDate.getDay();
if (birthDay > currentDay) {
console.log('birthDay > currentDay', yDiff, { birthDay, currentDay });
return yDiff - 1;
}
if (birthDay < currentDate) {
console.log('birthDay < currentDay', yDiff, { birthDay, currentDay });
return yDiff;
}
const currentHour = currentDate.getHours();
const birthtHour = birthdate.getHours();
if (birthtHour > currentHour) {
console.log('birthtHour > currentHour');
return yDiff - 1;
}
if (birthtHour < currentHour) {
console.log('birthtHour < currentHour');
return yDiff;
}
const currentSec = currentDate.getSeconds();
const birthtSec = birthdate.getSeconds();
if (birthtSec > currentSec) {
console.log('birthtSec > currentSec');
return yDiff - 1;
}
if (birthtSec < currentSec) {
console.log('birthtSec < currentSec');
return yDiff;
}
const currentMs = currentDate.getMilliseconds();
const birthtMs = birthdate.getMilliseconds();
if (birthtMs > currentMs) {
console.log('birthtMs > currentMs');
return yDiff - 1;
}
if (birthtMs < currentMs) {
console.log('birthtMs < currentMs');
return yDiff;
}
return yDiff;
// const currentDate = new Date();
// console.log({ birthdate });
// const currentDateMs = currentDate.getTime();
// const birthdateMs = birthdate.getTime();
// if (birthdateMs >= currentDateMs) {
// console.log('if kek', birthdate, birthdateMs, currentDateMs);
// return 0;
// }
// const ageMs = currentDateMs - birthdateMs;
// const ageD = ageMs / 1000 / 60 / 60 / 24 + 1;
// console.log({ ageD });
// const ageY = ageD / 365;
// // const ageY = Math.floor(ageMs / 1000 / 60 / 60 / 24 / daysInMonth / 12);
// console.log({ currentDate, birthdate, currentDateMs, birthdateMs, ageMs, ageY });
// return ageY;
}
function daysUntilBirthday(birthdate) {
return 0;
}
module.exports = {
calculateAge,
daysUntilBirthday,
};
// const currentDate = new Date();
// console.log({ birthdate });
// const currentDateMs = currentDate.getTime();
// const birthdateMs = birthdate.getTime();
// if (birthdateMs >= currentDateMs) {
// console.log('if kek', birthdate, birthdateMs, currentDateMs);
// return 0;
// }
// const ageMs = currentDateMs - birthdateMs;
// const ageD = ageMs / 1000 / 60 / 60 / 24 + 1;
// console.log({ ageD });
// const ageY = ageD / 365;
// // const ageY = Math.floor(ageMs / 1000 / 60 / 60 / 24 / daysInMonth / 12);
// console.log({ currentDate, birthdate, currentDateMs, birthdateMs, ageMs, ageY });
// return ageY;
Code will run automatically after changes.
Code runs in an isolated environment.
Code runs in an isolated environment.