Skip to content

Commit

Permalink
fix relative time problems
Browse files Browse the repository at this point in the history
  • Loading branch information
rlindner81 committed Oct 21, 2023
1 parent 2effbd4 commit f74d972
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions frontend/src/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const _getRelativeDateTime = (d1, d2) => {
let unit, limit, diff;
for (const unitLimitPair of Object.entries(TIME_UNIT_LIMITS)) {
[unit, limit] = unitLimitPair;
if (Math.abs(elapsed) > limit || unit === TIME_UNITS.SECOND) {
diff = Math.round(elapsed / limit);
if (Math.abs(elapsed) >= limit || unit === TIME_UNITS.SECOND) {
diff = Math.trunc(elapsed / limit);
break;
}
}
Expand Down Expand Up @@ -58,9 +58,8 @@ export const readableShortDateTime = (value: Date): string => {
return `${day}.${month}.${year}`;
};

export const readableRelativeDateTime = (value: Date): string => {
export const readableRelativeDateTime = (value: Date, now: Date = new Date()): string => {
if (!value) return "";
const now = new Date();
const relativeMillis = value.getTime() - now.getTime();
if (-NOW_TOLERANCE <= relativeMillis && relativeMillis <= NOW_TOLERANCE) {
return "now";
Expand Down

0 comments on commit f74d972

Please sign in to comment.