generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 165
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
1 parent
ab97ecf
commit 738a730
Showing
1 changed file
with
15 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
import { | ||
TOTAL_MILLISECONDS_IN_A_DAY, | ||
TOTAL_MILLISECONDS_IN_A_HOUR, | ||
} from '@/constants/date'; | ||
import { TOTAL_MILLISECONDS_IN_A_HOUR } from '@/constants/date'; | ||
|
||
const getIdleSinceText = (idleSince: string) => { | ||
const presentDate = new Date(); | ||
const idleSinceDate = new Date(idleSince); | ||
const differenceInDay = Math.round( | ||
(presentDate.setUTCHours(0, 0, 0, 0) - | ||
idleSinceDate.setUTCHours(0, 0, 0, 0)) / | ||
TOTAL_MILLISECONDS_IN_A_DAY | ||
); | ||
const differenceInHours = Math.abs( | ||
Math.round( | ||
(presentDate.getTime() - parseInt(idleSince)) / | ||
TOTAL_MILLISECONDS_IN_A_HOUR | ||
) | ||
|
||
const presentDateInUtc = presentDate.toUTCString(); | ||
const idleSinceDateInUtc = idleSinceDate.toUTCString(); | ||
|
||
const differenceInHours = Math.round( | ||
(new Date(presentDateInUtc).valueOf() - | ||
new Date(idleSinceDateInUtc).valueOf()) / | ||
TOTAL_MILLISECONDS_IN_A_HOUR | ||
); | ||
const idealDays = Math.round(differenceInHours / 24); | ||
|
||
if (differenceInDay > 1) { | ||
return `${differenceInDay} days ago`; | ||
} else { | ||
if (idealDays <= 0) { | ||
return `${differenceInHours} hours ago`; | ||
} else if (idealDays === 1) { | ||
return `${idealDays} day ago`; | ||
} | ||
|
||
return `${idealDays} days ago`; | ||
}; | ||
|
||
export default getIdleSinceText; |