Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

// Get years months and days from a date -

// + if 1 year is there then print 1 year and if 2 years is there it will print 2
years, simmarly for months and days

function calculateDateDifference(inputDate) {
const currentDate = new Date();
const inputDateObj = new Date(inputDate);

const yearDiff = currentDate.getFullYear() - inputDateObj.getFullYear();


const monthDiff = currentDate.getMonth() - inputDateObj.getMonth();
const dayDiff = currentDate.getDate() - inputDateObj.getDate();

let years = yearDiff;


let months = monthDiff;
let days = dayDiff;

if (dayDiff < 0) {
months--;p
const lastMonth = new Date(currentDate.getFullYear(),
currentDate.getMonth() - 1, inputDateObj.getDate());
days = Math.floor((currentDate - lastMonth) / (1000 * 60 * 60 * 24));
}

if (monthDiff < 0) {
years--;
months += 12;
}

return { years, months, days };


}1

const inputDate = "2022-06-27T00:00:00.000+00:00";


const dateDifference = calculateDateDifference(inputDate);

// console.log(`Years: ${dateDifference.years}`);
// console.log(`Months: ${dateDifference.months}`);
// console.log(`Days: ${dateDifference.days}`);

lastVisited = "";
if (dateDifference.years > 0 && dateDifference.years > 1) {
lastVisited += dateDifference.years + " years ";
} else if (dateDifference.years == 1) {
lastVisited += dateDifference.years + " year ";
}

if (dateDifference.months && dateDifference.months > 1) {


lastVisited += dateDifference.months + " months ";
} else if (dateDifference.months) {
lastVisited += dateDifference.months + " month ";
}
if (dateDifference.days && dateDifference.days > 1) {
lastVisited += dateDifference.days + " days ";
} else if (dateDifference.days) {
lastVisited += dateDifference.days + " day ";
}
lastVisited+="ago.";

if(!dateDifference.years && !dateDifference.months && !dateDifference.days){


lastVisited="Today";
}
console.log(lastVisited)

// function calculateDateDifference(inputDate) {
// const currentDate = new Date();
// const inputDateObj = new Date(inputDate);

// const yearDiff = currentDate.getFullYear() - inputDateObj.getFullYear();


// const monthDiff = currentDate.getMonth() - inputDateObj.getMonth();
// const dayDiff = currentDate.getDate() - inputDateObj.getDate();

// let years = yearDiff;


// let months = monthDiff;
// let days = dayDiff;

// if (dayDiff < 0) {
// months--;p
// const lastMonth = new Date(currentDate.getFullYear(),
currentDate.getMonth() - 1, inputDateObj.getDate());
// days = Math.floor((currentDate - lastMonth) / (1000 * 60 * 60 * 24));
// }

// if (monthDiff < 0) {
// years--;
// months += 12;
// }

// let lastVisited = "";


// if (years > 0 && years > 1) {
// lastVisited += years + " years ";
// } else if (years == 1) {
// lastVisited += years + " year ";
// }

// if (months && months > 1) {


// lastVisited += months + " months ";
// } else if (months) {
// lastVisited += months + " month ";
// }
// if (days && days > 1) {
// lastVisited += days + " days ";
// } else if (days) {
// lastVisited += days + " day ";
// }
// lastVisited+="ago.";

// if(!years && !months && !days){


// lastVisited="Today";
// }
// return lastVisited;
// }
// const inputDate = "2022-06-27T00:00:00.000+00:00";
// const lastVisited = calculateDateDifference(inputDate);
// console.log(lastVisited)

You might also like