Professional Documents
Culture Documents
Get Year Months Days From Date
Get Year Months Days From 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);
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;
}
// 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 ";
}
// function calculateDateDifference(inputDate) {
// const currentDate = new Date();
// const inputDateObj = new Date(inputDate);
// 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;
// }