Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: idle page time formatting #970

Merged
merged 10 commits into from
Oct 28, 2023
35 changes: 16 additions & 19 deletions src/helperFunctions/getIdleSinceText.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
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 = new Date().toUTCString();
const idleSinceDateInUtc = new Date(idleSince).toUTCString();

const presentDateMillisecond = new Date(presentDateInUtc).valueOf();
const idleSinceDateMillisecond = new Date(idleSinceDateInUtc).valueOf();

const differenceInHours = Math.round(
(presentDateMillisecond - idleSinceDateMillisecond) /
TOTAL_MILLISECONDS_IN_A_HOUR
);
const idleDays = Math.round(differenceInHours / 24);

if (differenceInDay > 1) {
return `${differenceInDay} days ago`;
} else {
if (idleDays <= 0) {
return `${differenceInHours} hours ago`;
} else if (idleDays === 1) {
return `${idleDays} day ago`;
}

return `${idleDays} days ago`;
};

export default getIdleSinceText;
Loading