Skip to content

Instantly share code, notes, and snippets.

@raynirola
Created June 27, 2022 05:32
Show Gist options
  • Save raynirola/781973a5775d8218da6da373bac9062d to your computer and use it in GitHub Desktop.
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.
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