Created
June 27, 2022 05:32
-
-
Save raynirola/781973a5775d8218da6da373bac9062d to your computer and use it in GitHub Desktop.
Quick code snippet for calculating "X days ago" given a specific date, with localized language.
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
const rtf = new Intl.RelativeTimeFormat ("en", { | |
localeMatcher: "best fit", | |
numeric: "always", | |
style: "long", | |
}); | |
function getDifferenceInDays(fromDate, toDate) { | |
const diff = Math.floor( (fromDate - toDate) / (1000 * 60 * 60 * 24)); | |
return rtf.format (diff, "day"); | |
} | |
getDifferenceInDays(new Date('2022-05-31T16:33:07.000Z' ), new Date()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment