-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
2 changed files
with
55 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const Minute = 60 | ||
const Hour = 3600 | ||
const Day = 86400 | ||
const Month = 30 * 86400 | ||
const Year = 365 * 86400 | ||
|
||
export const shortenTime = (d: Date) => { | ||
const now = new Date() | ||
|
||
let duration = (now.getTime() - d.getTime()) / 1000 | ||
|
||
if (duration < Minute) { | ||
return 'a few seconds ago' | ||
} | ||
|
||
if (duration < Hour) { | ||
duration = Number.parseInt(String(duration / Minute)) | ||
if (duration === 1) { | ||
return 'a minute ago' | ||
} | ||
return duration + ' minutes ago' | ||
} | ||
|
||
if (duration < Day) { | ||
duration = Number.parseInt(String(duration / Hour)) | ||
if (duration === 1) { | ||
return 'an hour ago' | ||
} | ||
return duration + ' hours ago' | ||
} | ||
|
||
if (duration < Month) { | ||
duration = Number.parseInt(String(duration / Day)) | ||
if (duration === 1) { | ||
return 'a day ago' | ||
} | ||
return duration + ' days ago' | ||
} | ||
|
||
if (duration < Year) { | ||
duration = Number.parseInt(String(duration / Month)) | ||
if (duration === 1) { | ||
return 'a month ago' | ||
} | ||
return duration + ' months ago' | ||
} | ||
|
||
duration = Number.parseInt(String(duration / Year)) | ||
if (duration === 1) { | ||
return 'a year ago' | ||
} | ||
return duration + ' years ago' | ||
} |