Created
October 17, 2023 15:22
-
-
Save louicoder/8e630046828673f81c621c5e4bf8ed74 to your computer and use it in GitHub Desktop.
Get difference between two dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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