Skip to content

Instantly share code, notes, and snippets.

@manakuro
Last active March 30, 2020 12:15
Show Gist options
  • Save manakuro/d9a32fd6505bc691c25786b7ab2c24bf to your computer and use it in GitHub Desktop.
Save manakuro/d9a32fd6505bc691c25786b7ab2c24bf to your computer and use it in GitHub Desktop.
export const formatDate = (date) => {
let result
if (date) {
const dateObj = new Date(date)
if (isToday(dateObj)) {
result = 'Today'
} else if (isYesterday(dateObj)) {
result = 'Yesterday'
} else if (!isThisYear(dateObj)) {
result = format(dateObj, 'MMMM d, yyyy')
} else {
result = format(dateObj, 'MMMM d')
}
} else {
throw new Error('No date')
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment