Last active
November 8, 2023 07:50
-
-
Save dninomiya/d57c1f49a5e32b10f6d434e4aab0b2c6 to your computer and use it in GitHub Desktop.
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 { differenceInDays, format, formatDistanceToNow } from 'date-fns'; | |
import { ja } from 'date-fns/locale'; | |
export default function RelativeTimestamp({ date }: { date: Date }) { | |
const diffInDays = differenceInDays(new Date(), date); | |
if (diffInDays < 8) { | |
return formatDistanceToNow(date, { | |
locale: ja, | |
addSuffix: true, | |
}); | |
} else if (diffInDays < 365) { | |
return format(date, 'MM月dd日'); | |
} else { | |
return format(date, 'yyyy年MM月dd日'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment