Skip to content

Instantly share code, notes, and snippets.

@louicoder
Created October 17, 2023 15:22
Show Gist options
  • Save louicoder/8e630046828673f81c621c5e4bf8ed74 to your computer and use it in GitHub Desktop.
Save louicoder/8e630046828673f81c621c5e4bf8ed74 to your computer and use it in GitHub Desktop.
Get difference between two dates
function formatDateDifference(isoDateString) {
const inputDate = new Date(isoDateString);
const currentDate = new Date();
const timeDifference = currentDate - inputDate;
// Calculate the number of years
const years = Math.floor(timeDifference / (365.25 * 24 * 60 * 60 * 1000));
// Calculate the number of months
const months = Math.floor(
(timeDifference % (365.25 * 24 * 60 * 60 * 1000)) /
(30.44 * 24 * 60 * 60 * 1000)
);
// Calculate the number of days
const days = Math.floor(
(timeDifference % (30.44 * 24 * 60 * 60 * 1000)) / (24 * 60 * 60 * 1000)
);
return `${years} years, ${months} months, ${days} days`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment