Created
January 14, 2022 19:31
-
-
Save gsans/aef2fac44f58e11fb50eca62f68ea32e to your computer and use it in GitHub Desktop.
codez048: messages time filter
This file contains 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
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