Skip to content

Commit e95d91a

Browse files
committed
fix: timestamp
1 parent 00a7d25 commit e95d91a

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

utils/functions/timestamp-parser.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ export const timestampParser = (
1717
timestamp: Date,
1818
type: "descriptive" | "YYYY-MM-DD" | "relative"
1919
): string => {
20+
const _unix: Date = new Date(timestamp);
21+
2022
switch (type) {
2123
case "descriptive": {
22-
const _unix: Date = new Date(timestamp);
23-
2424
const _minutes = _unix.getMinutes();
2525
const _hours = _unix.getHours();
2626
const _seconds = _unix.getSeconds();
@@ -49,17 +49,14 @@ export const timestampParser = (
4949

5050
case "relative": {
5151
const currentTime = new Date();
52-
const pastTime = new Date(timestamp);
52+
const timeDifference = currentTime.getTime() - _unix.getTime();
5353

54-
const yearsDifference =
55-
currentTime.getFullYear() - pastTime.getFullYear();
56-
const monthsDifference =
57-
currentTime.getMonth() - pastTime.getMonth();
58-
const daysDifference = currentTime.getDate() - pastTime.getDate();
59-
const hoursDifference =
60-
currentTime.getHours() - pastTime.getHours();
61-
const minutesDifference =
62-
currentTime.getMinutes() - pastTime.getMinutes();
54+
const secondsDifference = Math.floor(timeDifference / 1000);
55+
const minutesDifference = Math.floor(secondsDifference / 60);
56+
const hoursDifference = Math.floor(minutesDifference / 60);
57+
const daysDifference = Math.floor(hoursDifference / 24);
58+
const monthsDifference = Math.floor(daysDifference / 30);
59+
const yearsDifference = Math.floor(monthsDifference / 12);
6360

6461
if (yearsDifference > 0) {
6562
return `${yearsDifference} year${

0 commit comments

Comments
 (0)