@@ -17,10 +17,10 @@ export const timestampParser = (
17
17
timestamp : Date ,
18
18
type : "descriptive" | "YYYY-MM-DD" | "relative"
19
19
) : string => {
20
+ const _unix : Date = new Date ( timestamp ) ;
21
+
20
22
switch ( type ) {
21
23
case "descriptive" : {
22
- const _unix : Date = new Date ( timestamp ) ;
23
-
24
24
const _minutes = _unix . getMinutes ( ) ;
25
25
const _hours = _unix . getHours ( ) ;
26
26
const _seconds = _unix . getSeconds ( ) ;
@@ -49,17 +49,14 @@ export const timestampParser = (
49
49
50
50
case "relative" : {
51
51
const currentTime = new Date ( ) ;
52
- const pastTime = new Date ( timestamp ) ;
52
+ const timeDifference = currentTime . getTime ( ) - _unix . getTime ( ) ;
53
53
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 ) ;
63
60
64
61
if ( yearsDifference > 0 ) {
65
62
return `${ yearsDifference } year${
0 commit comments