-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from Full-Stack-Collective/appt-requested-time
Caluculate time between dates for appt requests
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 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,31 @@ | ||
const ONE_DAY_IN_MILLISECONDS = 86400000; | ||
const ONE_HOUR_IN_MILLISECONDS = 3600000; | ||
|
||
function getDaysBetweenDates(timeBetween: number) { | ||
const daysBetween = timeBetween / ONE_DAY_IN_MILLISECONDS; | ||
const days = | ||
daysBetween >= 0 ? Math.floor(daysBetween) : Math.ceil(daysBetween); | ||
|
||
if (days === 1) return `Requested 1 day ago`; | ||
return `Requested ${days} days ago`; | ||
} | ||
|
||
function getHoursBetweenDates(timeBetween: number) { | ||
const hoursBetween = timeBetween / ONE_HOUR_IN_MILLISECONDS; | ||
const hours = | ||
hoursBetween >= 0 ? Math.floor(hoursBetween) : Math.ceil(hoursBetween); | ||
|
||
if (hours > 24) return getDaysBetweenDates(timeBetween); | ||
|
||
if (hours < 1) return `Requested < 1 hour ago`; | ||
else if (hours === 1) return `Requested 1 hour ago`; | ||
return `Requested ${hours} hours ago`; | ||
} | ||
|
||
export function updateRequestCreatedTime(dateCreated: string) { | ||
const today = new Date(); | ||
const requestCreated = new Date(dateCreated); | ||
const timeBetween = today.getTime() - requestCreated.getTime(); | ||
|
||
return getHoursBetweenDates(timeBetween); | ||
} |