dob format is 2022-07
desired output is 0 Years 5 Months
Below is the code I tried but I am getting months in negative.
export default function calculateAge(date) {
let month = new Date().getMonth() - Number(date.split("-")[1]);
let year = new Date().getFullYear() - Number(date.split("-")[0]);
console.log(`month is`, month);
if (month < 0 && year < 1) {
month = year * 12 month;
year = 0;
}
console.log(`year`, year);
return `${year ? `${year} Year${year > 1 ? `s` : ""}` : ""} ${
month ? `${month} Month${month > 1 ? "s" : ""}` : ""
}`;
}
CodePudding user response:
function calculateAge(date) {
const now = new Date()
const then = new Date(...date.split('-'))
const diff = new Date(now - then)
const months = diff.getMonth()
const years = diff.getUTCFullYear() - 1970
return `${years} ${years !== 1 ? 'Years' : 'Year'} ${months} ${months !== 1 ? 'Months' : 'Month'}`
}
const age = calculateAge('2010-05')
console.log(age)
CodePudding user response:
function calculateAge(date) {
// Split the date string into year and month
const [year, month] = date.split("-");
// Get the current year and month
const currentYear = new Date().getFullYear();
const currentMonth = new Date().getMonth() 1; // months are 0-indexed in JavaScript
// Calculate the difference in years and months
let ageYears = currentYear - Number(year);
let ageMonths = currentMonth - Number(month);
// If the age in months is negative, subtract 1 from the age in years and add 12 to the age in months
if (ageMonths < 0) {
ageYears -= 1;
ageMonths = 12;
}
// Return the age in years and months as a string
return `${ageYears ? `${ageYears} Year${ageYears > 1 ? `s` : ""}` : ""} ${
ageMonths ? `${ageMonths} Month${ageMonths > 1 ? "s" : ""}` : ""
}`;
}
CodePudding user response:
You are missing the "modulo" exprerience Remainder (%)
and calculate the difference in number of months (1 year = 12 months)
then convert the result to years / months
as none of the previous answers seem correct to me...
function calculateAge( start_date, end_Date ) // end_Date is optionnal
{
const
[ yRef, mRef ] = start_date.split('-').map(Number)
, today = new Date()
, [ yEnd, mEnd ] = !!end_Date
? end_Date.split('-').map(Number)
: [ today.getFullYear(), today.getMonth() 1 ]
, delta_Months = ((yEnd - yRef) * 12) mEnd -mRef
, months = delta_Months % 12
, years = (delta_Months - months) / 12
;
return `${years} Year(s), ${months} Month(s)`
}
I also added more examples so that everyone can check the correctness of the result.
// actual date on code writing is 2022-12
// PO test values . . . . . . . = 2022-07
// PO expected return . . . . . = 0 Years 5 Months
console.log(calculateAge('2022-07')) // 0 Year(s), 5 Month(s)
console.log(('--- -- - --- -- - --- -- - other tests'))
console.log(calculateAge('2010-05')) // 12 Year(s), 7 Month(s)
console.log(calculateAge('2010-10')) // 12 Year(s), 2 Month(s)
console.log(calculateAge('2010-11'))
console.log(calculateAge('2010-12')) // 12 Year(s), 0 Month(s)
console.log(calculateAge('2011-01'))
console.log(calculateAge('2011-02')) // 11 Year(s), 10 Month(s)
console.log(('--- -- - --- -- - --- -- - test with specifics ending date '))
console.log(calculateAge('2020-01','2022-06')) // 2 Year(s), 5 Month(s)
console.log(calculateAge('2020-06','2022-06')) // 2 Year(s), 0 Month(s)
console.log(calculateAge('2020-06','2022-01')) // 1 Year(s), 7 Month(s)
function calculateAge( start_date, end_Date ) // end_Date is optionnal
{
const
[ yRef, mRef ] = start_date.split('-').map(Number)
, today = new Date()
, [ yEnd, mEnd ] = !!end_Date
? end_Date.split('-').map(Number)
: [ today.getFullYear(), today.getMonth() 1 ]
, delta_Months = ((yEnd - yRef) * 12) mEnd -mRef
, months = delta_Months % 12
, years = (delta_Months - months) / 12
;
return `${years} Year(s), ${months} Month(s)`
}
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}