init source
This commit is contained in:
41
utils/formatDate.ts
Normal file
41
utils/formatDate.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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);
|
||||
};
|
Reference in New Issue
Block a user