import { format } from "date-fns"; export default class DateFormatter { /** * date를 인자로 넘기면 9시간 빼서 formatValue대로 foramt해서 return해준다. * * new Date("2023-12-21"), "yyyy-MM-dd HH:mm:ss" -> "2023-12-21 00:00:00" * null, "yyyy-MM-dd" -> "-" * * @public * @return string * @param value date * @param formatValue ex) 2023-12-22 3:00:50 */ static divideDateDuration(value: any, formatValue: string) { if (!value) return "-"; const temp = new Date(value); temp.setHours(temp.getHours() - 9); return format(temp, formatValue); } /** * 현재 시간에서 1시간을 더하고 분은 10분 단위로 표시하며 10분보다 적을 경우 10으로 맞추고, 10보다 클 경우 분의 첫번째자리는 버리고 yyyy-MM-dd HH:mm:ss로 format해서 return한다. * * 2023-01-01 13:03:00 -> 2023-01-01 22:10:00 * 2023-01-01 13:45:00 -> 2023-01-01 22:40:00 * * @public * @static * @return string */ static getAfterOneHourAnd10MinutesInterval() { const date = new Date(); date.setHours(date.getHours() + 1); if (date.getMinutes() < 10) { date.setMinutes(date.getMinutes() + 10 - date.getMinutes()); } else { date.setMinutes(Math.floor(date.getMinutes() / 10) * 10 + 10); } return format(date, "yyyy-MM-dd HH:mm:ss"); } /** * 인자값으로 2개의 날짜를 넣으면 yyyy-MM-DD - yyyy-MM-DD 형식으로 return한다. * * [new Date("2023-12-21"), new Date("2023-12-22")] -> "2023-12-21 - 2023-12-22" * [new Date("2023-12-21")] -> "" * * @public * @return string yyyy-MM-DD - yyyy-MM-DD * @param date Date[] */ static getCalendarDateDurationFormat(date: Array) { if (date.length < 2) return ""; const startDay = date[0].getDate(); const startMonth = date[0].getMonth() + 1; const startYear = date[0].getFullYear(); const endDay = date[1].getDate(); const endMonth = date[1].getMonth() + 1; const endYear = date[1].getFullYear(); return `${startYear}-${startMonth}-${ startDay < 10 ? "0" + startDay : startDay } - ${endYear}-${endMonth}-${endDay < 10 ? "0" + endDay : endDay}`; } /** * 인자로 넘긴 date에서 9시간을 뺀 후 type을 string으로 변경해 return하며 date에 값이 없을 경우 null을 return한다. * * "2023-12-21 13:00:00" -> "Thu Dec 21 2023 04:00:00 GMT+0900 (Korean Standard Time)" * undefined -> null * * @static * @public * @param date * @return string */ static getDatePickerConvertedDate(date: string) { if (!date) { return null; } else { const newDate = new Date(date); newDate.setHours(newDate.getHours() - 9); return String(newDate); } } /** * formatValue를 인자로 넘기면 현재 시간을 formatValue에 따라 format해 return한다. * * "yyyy-MM-dd" -> "2024-01-08" * * @static * @public * @return string * @param formatValue */ static getNowDate(formatValue: string) { const newDate = new Date(); // newDate ? (this.date = format(newDate, formatValue)) : (this.date = "-"); return format(newDate, formatValue); } /** * startDate와 endDate를 넘기면 yyyy-MM-dd ~ yyyy-MM-dd format으로 convert해 return하고 인자로 startDate가 null이면 - return한다. * * "2023-12-21", "2023-12-22" -> "2023-12-21 ~ 2023-12-22" * null, "2023-12-25" -> "-" * * @public * @return string * @param startDate * @param endDate */ static getOperatingPauseDuration( startDate: string | null, endDate: string | null ) { if (!startDate) return "-"; return `${format(new Date(startDate), "yyyy-MM-dd")} ~ ${ endDate ? format(new Date(endDate), "yyyy-MM-dd") : "" }`; } /** * null일 경우 빈 스트링을 return하고 date를 넘기면 9시간을 뺀 후 yyy-MM-dd format으로 return한다. * * "2023-12-21 13:00:00" -> "2023-12-21" * null -> "" * * @public * @static * @return string * @param date */ static insertDashBarInDate(date: string) { if (!date) return ""; const newDate = new Date(date); newDate.setHours(newDate.getHours() - 9); return format(newDate, "yyyy-MM-dd"); } /** * date를 넘기면 9시간을 빼고 yyyy-MM-dd HH:mm:ss format으로 return하고 date가 없을 경우 "-" return한다. * * "2023-12-21" -> "2023-12-21 00:00:00" * null -> "-" * * @public * @static * @return string * @param date */ static insertDashBarInDateAndTime(date: string) { if (!date) return "-"; const newDate = new Date(date); newDate.setHours(newDate.getHours() - 9); return format(newDate, "yyyy-MM-dd HH:mm:ss"); } /** * 인자로 넘겨진 date에서 9시간을 뺀 후 return한다. * * new Date("2023-12-20T10:00:00Z") -> new Date("2023-12-20T01:00:00.000Z") * * @public * @static * @return string * @param date */ static matchDateWithServer(date: Date) { date.setHours(date.getHours() - 9); return date; } /** * value 인자값이 없으면 "-" return 있으면 foramtValue에 맞춰 format해 return한다. * * "2024-01-01", "yyyy/MM/dd" -> "2024/01/01" * null, "yyyy-MM-dd" -> "-" * * @public * @static * @return string * @param value * @param formatValue */ static noSetKoreanHour(value: any, formatValue: string) { try { return value ? format(new Date(value), formatValue) : "-"; } catch (e) {} } }