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
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 idleDays = Math.round(differenceInHours / 24);

if (differenceInDay > 1) {
return `${differenceInDay} days ago`;
} else {
if (idealDays <= 0) {
return `${differenceInHours} hours ago`;
yesyash marked this conversation as resolved.
Show resolved Hide resolved
} else if (idealDays === 1) {
return `${idealDays} day ago`;
yesyash marked this conversation as resolved.
Show resolved Hide resolved
}
yesyash marked this conversation as resolved.
Show resolved Hide resolved

return `${idealDays} days ago`;
};
yesyash marked this conversation as resolved.
Show resolved Hide resolved

export default getIdleSinceText;
Loading