Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/utils/datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const ONE_HOUR_MS = 3_600_000;

/**
* "YYYY-MM-DD" 형식의 한국식 날짜 문자열을 반환
* @param d - Date 객체
*/
export function formatDate(d: Date): string {
return d
.toLocaleDateString("ko-KR", {
year: "numeric",
month: "2-digit",
day: "2-digit",
})
.replace(/\.\s?/g, "-")
.slice(0, 10);
}

/**
* "HH:MM" 형식의 24시간제 시간 문자열을 반환
* @param d - Date 객체
*/
export function formatTime(d: Date): string {
return d.toLocaleTimeString("ko-KR", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
});
}

/**
* "YYYY-MM-DD HH:MM~HH:MM (N시간)" 형식의 시간 범위 문자열을 반환
* @param startsAt - ISO 8601 형식의 날짜 문자열
* @param workhour - 근무 시간 (시간 단위)
*/
export function formatTimeRange(startsAt: string, workhour: number): string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그냥 단순히 궁금한 부분입니다만,
startAt이 아니라 startsAt인가요..! 😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그리고 입력받는 startsAtstring타입이라면
전달 받아야 하는 특정 형태가 있을까요? 🤔 (ex: YYYY-MM-DD HH:mm 등)
있다면 JSDoc을 사용해서 어떤 형태로 전달 받아야 하는지에 대한 설명이 포함되면 좋겠어요! 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api 리스폰스가 startsAt으로 오는 것 같아서 startsAt으로 지정하기는 했습니다..!😊

const start = new Date(startsAt);
const end = new Date(start.getTime() + workhour * ONE_HOUR_MS);

return `${formatDate(start)} ${formatTime(start)}~${formatTime(end)} (${workhour}시간)`;
}

/**
* 해당 공고가 과거에 종료되었는지를 판단
* @param startsAt - ISO 8601 형식의 날짜 문자열
* @param workhour - 근무 시간 (시간 단위)
*/
export function isPastDate(startsAt: string, workhour: number): boolean {
const start = new Date(startsAt);
const end = new Date(start.getTime() + workhour * ONE_HOUR_MS);
return end.getTime() < Date.now();
}