Created
March 30, 2020 12:08
-
-
Save manakuro/685135588e670f52d32000083ddd1ea3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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