1
1
import { useMemo } from "react" ;
2
-
3
2
import { useTranslation } from "react-i18next" ;
4
3
5
- const NOW = new Date ( ) ;
6
-
7
- const formatTooltip = ( date : Date ) =>
8
- date
9
- . toLocaleString ( "fr-FR" , {
10
- day : "2-digit" ,
11
- month : "2-digit" ,
12
- year : "2-digit" ,
13
- hour : "2-digit" ,
14
- minute : "2-digit" ,
15
- second : "2-digit" ,
16
- } )
17
- . replace ( "," , "" ) ;
18
-
19
- const formatDate = ( date : Date ) =>
20
- date . toLocaleString ( "fr-FR" , {
21
- day : "2-digit" ,
22
- month : "2-digit" ,
23
- year : "2-digit" ,
24
- } ) ;
25
-
26
4
export const useElapsedTime = ( dateString : string ) => {
27
5
const { t } = useTranslation ( "magneto" ) ;
28
6
29
7
return useMemo ( ( ) => {
30
8
const date = new Date ( dateString ) ;
31
- const diffInSeconds = Math . floor ( ( NOW . getTime ( ) - date . getTime ( ) ) / 1000 ) ;
32
- const tooltip = formatTooltip ( date ) ;
9
+ const now = new Date ( ) ;
10
+ const diffInSeconds = Math . floor ( ( now . getTime ( ) - date . getTime ( ) ) / 1000 ) ;
11
+
12
+ if ( dateString ) {
13
+ console . log ( "outside===============" ) ;
14
+ console . log ( "date" ) ;
15
+ console . log ( dateString ) ;
16
+ console . log ( date ) ;
17
+ console . log ( "getTime" , date . getTime ( ) ) ;
18
+ console . log ( "now" ) ;
19
+ console . log ( now ) ;
20
+ console . log ( "toLocaleString" , now . toLocaleString ( ) ) ;
21
+ console . log ( "valueOf" , now . valueOf ( ) ) ;
22
+ console . log ( "getTime" , now . getTime ( ) ) ;
23
+ }
33
24
25
+ const tooltip = date
26
+ . toLocaleString ( "fr-FR" , {
27
+ day : "2-digit" ,
28
+ month : "2-digit" ,
29
+ year : "2-digit" ,
30
+ hour : "2-digit" ,
31
+ minute : "2-digit" ,
32
+ second : "2-digit" ,
33
+ } )
34
+ . replace ( "," , "" ) ;
34
35
if ( diffInSeconds < 60 ) {
35
36
return {
36
37
label :
@@ -40,7 +41,6 @@ export const useElapsedTime = (dateString: string) => {
40
41
tooltip,
41
42
} ;
42
43
}
43
-
44
44
if ( diffInSeconds < 3600 ) {
45
45
const diffInMinutes = Math . floor ( diffInSeconds / 60 ) ;
46
46
return {
@@ -51,7 +51,6 @@ export const useElapsedTime = (dateString: string) => {
51
51
tooltip,
52
52
} ;
53
53
}
54
-
55
54
if ( diffInSeconds < 86400 ) {
56
55
const diffInHours = Math . floor ( diffInSeconds / 3600 ) ;
57
56
return {
@@ -62,16 +61,18 @@ export const useElapsedTime = (dateString: string) => {
62
61
tooltip,
63
62
} ;
64
63
}
65
-
66
64
if ( diffInSeconds < 172800 ) {
67
65
return {
68
66
label : t ( "magneto.card.time.yesterday" ) ,
69
67
tooltip,
70
68
} ;
71
69
}
72
-
73
70
return {
74
- label : formatDate ( date ) ,
71
+ label : date . toLocaleString ( "fr-FR" , {
72
+ day : "2-digit" ,
73
+ month : "2-digit" ,
74
+ year : "2-digit" ,
75
+ } ) ,
75
76
tooltip,
76
77
} ;
77
78
} , [ dateString ] ) ;
0 commit comments