-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
unknown
committed
Jul 7, 2024
1 parent
8f0e149
commit 4062c4b
Showing
4 changed files
with
59 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function compareTime(targetTime) { | ||
const target = Date.parse(targetTime); | ||
const now = Date.now(); | ||
|
||
return now - target; | ||
} | ||
|
||
export default compareTime; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import compareTime from "./compareTime"; | ||
|
||
const SEC = 1000; | ||
const MIN = 60 * SEC; | ||
const HOUR = 60 * MIN; | ||
const DAY = 24 * HOUR; | ||
|
||
function fitDigits(num) { | ||
if (num < 10) return `0${num}`; | ||
else return num; | ||
} | ||
|
||
function formatComparedTime(targetTime) { | ||
const comparedTime = compareTime(targetTime); | ||
|
||
if(comparedTime > 7 * DAY) { | ||
const date = new Date(targetTime); | ||
const hour = fitDigits(date.getHours()); | ||
const min = fitDigits(date.getMinutes()); | ||
const dateFormat = `${date.getFullYear()}년 ${date.getMonth() + 1}월 ${date.getDate()}일 ${hour}:${min}`; | ||
|
||
return dateFormat; | ||
} | ||
|
||
else if(comparedTime > 2 * DAY) return `${comparedTime / DAY}일 전`; | ||
|
||
else if(comparedTime > DAY) return "어제"; | ||
|
||
else if(comparedTime > HOUR) return `${comparedTime / HOUR}시간 전`; | ||
|
||
else if(comparedTime > MIN) return `${compareTime / MIN}분 전`; | ||
|
||
else return "방금 전"; | ||
} | ||
|
||
export default formatComparedTime; |