Skip to content
Merged
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions src/utils/datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//"YYYY-MM-DD" 형식의 한국식 날짜 문자열
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시간제 시간 문자열로 변환
export function formatTime(d: Date): string {
return d.toLocaleTimeString("ko-KR", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
});
}
// 시작 시간 + workhour 기준으로 endTime까지 포함한 시간 범위 문자열
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 * 3600000);
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. 매직넘버는 별도의 상수화를 통해서 그 의미를 전달하면 좋을 것 같습니다 🤔
const ONE_MINUTE = 3_600_000;
...

  const end = new Date(start.getTime() + workhour * ONE_MINUTE);
  1. 언더스코어(_)를 통해서 숫자 구분을 해도 좋을 것 같아요! 숫자 데이터와 동일하게 취급돼요! 😄 (ex: 3_600_000)

return `${formatDate(start)} ${formatTime(start)}~${formatTime(end)} (${workhour}시간)`;
}
// 시작 시간과 종료 시간을 기준으로 과거 여부 판단
export function isPastDate(startsAt: string, workhour: number): boolean {
const start = new Date(startsAt);
const end = new Date(start.getTime() + workhour * 3600000);
return end.getTime() < Date.now();
}