Skip to content

Instantly share code, notes, and snippets.

@manakuro
Created March 30, 2020 12:08
Show Gist options
  • Save manakuro/685135588e670f52d32000083ddd1ea3 to your computer and use it in GitHub Desktop.
Save manakuro/685135588e670f52d32000083ddd1ea3 to your computer and use it in GitHub Desktop.
export const formatDate = (date) => {
if (!date) throw new Error('No date') // If no date, throw an error
const dateObj = new Date(date)
if (isToday(dateObj)) return 'Today' // If the date is today, return 'Today'
if (isYesterday(dateObj)) return 'Yesterday' // If the date is yesterday, return 'Yesterday'
if (!isThisYear(dateObj)) return format(dateObj, 'MMMM d, yyyy') // If the date is not in this year, return date with year
return format(dateObj, 'MMMM d') // If no matching the above, return date with month and date
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment