@@ -2,23 +2,31 @@ export const calcTime = (dateString: string | undefined): string => {
22 if ( ! dateString ) return "unknown time" ;
33
44 try {
5- const date = new Date ( dateString ) ;
5+ // 1. If the string contains 'T' but doesn't have a timezone offset,
6+ // or if we want to treat the 'Z' as local time to match the user's intent:
7+ let formattedString = dateString ;
8+ if ( dateString . includes ( "T" ) && dateString . endsWith ( "Z" ) ) {
9+ // Remove the 'Z' so the browser interprets this as LOCAL time, not UTC
10+ formattedString = dateString . slice ( 0 , - 1 ) ;
11+ }
12+
13+ const date = new Date ( formattedString ) ;
614 if ( isNaN ( date . getTime ( ) ) ) throw new Error ( "Invalid date" ) ;
715
816 const now = new Date ( ) ;
17+
18+ // 2. Standardize both to ignore milliseconds for cleaner math
919 const diffInSeconds = Math . floor ( ( now . getTime ( ) - date . getTime ( ) ) / 1000 ) ;
1020
11- // 1. Check if the date is in the FUTURE
1221 const isFuture = diffInSeconds < 0 ;
13- const absDiff = Math . abs ( diffInSeconds ) ; // Use Absolute Value to remove the '-'
22+ const absDiff = Math . abs ( diffInSeconds ) ;
1423
15- // 2. Helper to format the string
1624 const formatLabel = ( value : number , unit : string ) => {
1725 return isFuture ? `in ${ value } ${ unit } ` : `${ value } ${ unit } ago` ;
1826 } ;
1927
20- // 3. Logic for relative time using the absolute difference
21- if ( absDiff < 60 ) return formatLabel ( absDiff , "seconds" ) ;
28+ // 3. Logic (Same as before)
29+ if ( absDiff < 60 ) return isFuture ? "due now" : "just now" ;
2230
2331 const diffInMinutes = Math . floor ( absDiff / 60 ) ;
2432 if ( diffInMinutes < 60 ) return formatLabel ( diffInMinutes , "minutes" ) ;
@@ -29,7 +37,7 @@ export const calcTime = (dateString: string | undefined): string => {
2937 const diffInDays = Math . floor ( diffInHours / 24 ) ;
3038 return formatLabel ( diffInDays , "days" ) ;
3139 } catch ( error ) {
32- return "invalid date: " + error ;
40+ return "invalid date" + error ;
3341 }
3442} ;
3543
0 commit comments