Skip to content

Instantly share code, notes, and snippets.

@dninomiya
Last active November 8, 2023 07:50
Show Gist options
  • Save dninomiya/d57c1f49a5e32b10f6d434e4aab0b2c6 to your computer and use it in GitHub Desktop.
Save dninomiya/d57c1f49a5e32b10f6d434e4aab0b2c6 to your computer and use it in GitHub Desktop.
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