roomescape-refactored/frontend/src/util/DateTimeFormatter.ts

35 lines
1.0 KiB
TypeScript

export const formatDate = (dateStr: string) => {
const date = new Date(dateStr);
const currentYear = new Date().getFullYear();
const reservationYear = date.getFullYear();
const days = ['일', '월', '화', '수', '목', '금', '토'];
const dayOfWeek = days[date.getDay()];
const month = date.getMonth() + 1;
const day = date.getDate();
let datePart = '';
if (currentYear === reservationYear) {
datePart = `${month}${day}일(${dayOfWeek})`;
} else {
datePart = `${reservationYear}${month}${day}일(${dayOfWeek})`;
}
return datePart;
};
export const formatTime = (timeStr: string) => {
const [hourStr, minuteStr] = timeStr.split(':');
let hours = parseInt(hourStr, 10);
const minutes = parseInt(minuteStr, 10);
const ampm = hours >= 12 ? '오후' : '오전';
hours = hours % 12;
hours = hours ? hours : 12;
let timePart = `${ampm} ${hours}`;
if (minutes !== 0) {
timePart += ` ${minutes}`;
}
return timePart;
}