Skip to content

Commit

Permalink
Refactor/#155: 생성 시간 포멧팅 n분 전, 방금 전 추가 (#156)
Browse files Browse the repository at this point in the history
* refactor: 방금 전, n분 전 형식 추가

* docs: n분 전 형식이 반영된 댓글 및 답글 사진 사용

* fix: 닉네임 우측에 콤마 표시 제거
  • Loading branch information
semnil5202 authored May 9, 2024
1 parent 55d6191 commit 662daa7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

관심이 가는 게시글에 댓글과 답글을 남겨보세요. 프로필을 클릭해서 상대방의 세부 역량을 쉽게 파악할 수도 있어요. 😇

![image](https://github.com/ConceptBe/conceptbe-frontend/assets/89172499/fc1c60f2-aac8-4bbc-9670-b9f95e4275fe)
![image](https://github.com/ConceptBe/conceptbe-frontend/assets/89172499/15fbe1e3-0143-4857-be7f-df28c2600fa8)

### 상세한 프로필 설정

Expand Down
2 changes: 1 addition & 1 deletion src/pages/Feed/Feed.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const Feed = () => {
<Spacer size={27} />
<FeedFixTextWrapper>
<Text font="suit22sb" color="w1">
{getUserNickname() || 'Guest1234'},
{getUserNickname() || 'Guest1234'}
</Text>
<Text font="suit22r" color="w1">
님,
Expand Down
29 changes: 19 additions & 10 deletions src/pages/Feed/utils/formatCommentDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ export function formatCommentDate(date: string) {
const now = new Date();
const commentDate = new Date(date);
const diffTime = Math.abs(now.getTime() - commentDate.getTime());
const diffMinutes = Math.floor(diffTime / (1000 * 60));
const diffHours = Math.floor(diffTime / (1000 * 60 * 60));

if (diffMinutes < 1) {
return '방금 전';
}

if (diffMinutes < 60) {
return `${diffMinutes}분 전`;
}

if (diffHours < 24) {
return `${diffHours}시간 전`;
} else {
const year = commentDate.getFullYear();
const month = commentDate.getMonth() + 1; // getMonth()는 0부터 시작하므로 +1
const day = commentDate.getDate();
const hours = commentDate.getHours();
const minutes = commentDate.getMinutes();
}

// 숫자가 한 자리일 때 앞에 0을 붙여주는 함수
const padZero = (num: number) => num.toString().padStart(2, '0');
const year = commentDate.getFullYear();
const month = commentDate.getMonth() + 1; // getMonth()는 0부터 시작하므로 +1
const day = commentDate.getDate();
const hours = commentDate.getHours();
const minutes = commentDate.getMinutes();

return `${year}.${padZero(month)}.${padZero(day)} ${padZero(hours)}:${padZero(minutes)}`;
}
// 숫자가 한 자리일 때 앞에 0을 붙여주는 함수
const padZero = (num: number) => num.toString().padStart(2, '0');

return `${year}.${padZero(month)}.${padZero(day)} ${padZero(hours)}:${padZero(minutes)}`;
}

0 comments on commit 662daa7

Please sign in to comment.