Skip to content

Commit

Permalink
fix getIdleSinceText function
Browse files Browse the repository at this point in the history
  • Loading branch information
anishpawaskar committed Oct 26, 2023
1 parent ab97ecf commit 738a730
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/helperFunctions/getIdleSinceText.ts
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;

0 comments on commit 738a730

Please sign in to comment.