Files
bio_frontend/utils/formatDate.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-08-08 13:11:33 +09:00
export const formatDate = (
date: Date | string,
options?: Intl.DateTimeFormatOptions
): string => {
const dateObj = typeof date === "string" ? new Date(date) : date;
const defaultOptions: Intl.DateTimeFormatOptions = {
year: "numeric",
month: "long",
day: "numeric",
};
return dateObj.toLocaleDateString("ko-KR", { ...defaultOptions, ...options });
};
export const formatRelativeTime = (date: Date | string): string => {
const dateObj = typeof date === "string" ? new Date(date) : date;
const now = new Date();
const diffInSeconds = Math.floor((now.getTime() - dateObj.getTime()) / 1000);
if (diffInSeconds < 60) {
return "방금 전";
}
const diffInMinutes = Math.floor(diffInSeconds / 60);
if (diffInMinutes < 60) {
return `${diffInMinutes}분 전`;
}
const diffInHours = Math.floor(diffInMinutes / 60);
if (diffInHours < 24) {
return `${diffInHours}시간 전`;
}
const diffInDays = Math.floor(diffInHours / 24);
if (diffInDays < 7) {
return `${diffInDays}일 전`;
}
return formatDate(dateObj);
};