Calculate Life Statistics
Calculate weeks lived, weeks remaining, and life milestones from a birthdate
Source Code
const [birthdate, lifeExpectancyArg = "90"] = process.argv.slice(2);
if (!birthdate) {
console.error("Error: birthdate is required (YYYY-MM-DD format)");
process.exit(1);
}
const lifeExpectancy = parseInt(lifeExpectancyArg, 10);
const WEEKS_PER_YEAR = 52;
const TOTAL_WEEKS = lifeExpectancy * WEEKS_PER_YEAR;
// Parse birthdate
const birth = new Date(birthdate);
if (isNaN(birth.getTime())) {
console.error("Error: Invalid birthdate format. Use YYYY-MM-DD");
process.exit(1);
}
const now = new Date();
const ageMs = now.getTime() - birth.getTime();
const ageWeeks = Math.floor(ageMs / (7 * 24 * 60 * 60 * 1000));
const ageYears = ageMs / (365.25 * 24 * 60 * 60 * 1000);
const ageDays = Math.floor(ageMs / (24 * 60 * 60 * 1000));
// Calculate weeks
const weeksLived = Math.min(ageWeeks, TOTAL_WEEKS);
const weeksRemaining = Math.max(0, TOTAL_WEEKS - weeksLived);
const percentageLived = ((weeksLived / TOTAL_WEEKS) * 100).toFixed(1);
// Calculate milestones
const seasonsLived = Math.floor(ageWeeks / 13);
const decadesLived = Math.floor(ageYears / 10);
const yearsLived = Math.floor(ageYears);
const monthsLived = Math.floor(ageYears * 12);
// Calculate next milestone
const nextBirthday = new Date(birth);
nextBirthday.setFullYear(now.getFullYear());
if (nextBirthday < now) {
nextBirthday.setFullYear(now.getFullYear() + 1);
}
const daysUntilBirthday = Math.ceil(
(nextBirthday.getTime() - now.getTime()) / (24 * 60 * 60 * 1000)
);
// Format age nicely
const ageYearsWhole = Math.floor(ageYears);
const ageMonthsRemainder = Math.floor((ageYears - ageYearsWhole) * 12);
console.log("Life statistics calculated:");
console.log(` Age: ${ageYearsWhole} years, ${ageMonthsRemainder} months`);
console.log(` Weeks lived: ${weeksLived.toLocaleString()} of ${TOTAL_WEEKS.toLocaleString()}`);
console.log(` Progress: ${percentageLived}%`);
const stats = {
birthdate,
lifeExpectancy,
totalWeeks: TOTAL_WEEKS,
weeksLived,
weeksRemaining,
percentageLived: parseFloat(percentageLived),
age: {
years: ageYearsWhole,
months: monthsLived,
weeks: weeksLived,
days: ageDays,
formatted: `${ageYearsWhole} years, ${ageMonthsRemainder} months`,
},
milestones: {
seasonsLived,
yearsLived,
decadesLived,
daysUntilNextBirthday: daysUntilBirthday,
nextAge: ageYearsWhole + 1,
},
};
console.log(JSON.stringify(stats, null, 2));