Skip to content

Instantly share code, notes, and snippets.

@gsans
Created January 14, 2022 19:31
Show Gist options
  • Save gsans/aef2fac44f58e11fb50eca62f68ea32e to your computer and use it in GitHub Desktop.
Save gsans/aef2fac44f58e11fb50eca62f68ea32e to your computer and use it in GitHub Desktop.
codez048: messages time filter
import moment from 'moment';
moment.updateLocale('en', {
monthsShort: [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
],
});
export default function time(_date) {
const date = moment(_date);
const today = moment().startOf('day');
const yesterday = moment().subtract(1,'day').startOf('day');
if (date <= yesterday) { // Jan 31, 6:30 PM
return date.format("MMM D, h:mm A");
} else if (date <= today) { // Yesterday, 6:30 PM
return `Yesterday, ${date.format("h:mm A")}`;
} else { // 6:30 PM
return date.format("h:mm A");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment