Skip to content

Instantly share code, notes, and snippets.

@snewcomer
Created August 23, 2021 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snewcomer/cdcf65cac697a87194ab09071af97318 to your computer and use it in GitHub Desktop.
Save snewcomer/cdcf65cac697a87194ab09071af97318 to your computer and use it in GitHub Desktop.
Last Modified Date
function lastModifiedDate(updatedDate, intl) {
const today = new Date();
const yesterday = subDays(today, 1);
const aWeekAgo = subDays(today, 7);
const twoWeeksAgo = subDays(today, 14);
const threeWeeksAgo = subDays(today, 21);
const locNamespace = 'Last Updated';
// Get the date in English locale to match English day of week keys
const compare = parseISO(updatedDate);
let result = '';
if (isSameDay(compare, today)) {
result = intl.t(`${locNamespace}Today`);
} else if (isSameDay(compare, yesterday)) {
result = intl.t(`${locNamespace}Yesterday`);
} else if (isAfter(compare, aWeekAgo)) {
result = intl.t(`${locNamespace}${formatDate(compare, 'EEEE')}`);
} else if (isAfter(compare, twoWeeksAgo)) {
result = intl.t(`${locNamespace}LastWeek`);
} else if (isAfter(compare, threeWeeksAgo)) {
result = intl.t(`${locNamespace}TwoWeeksAgo`);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment