File tree Expand file tree Collapse file tree 2 files changed +5
-0
lines changed Expand file tree Collapse file tree 2 files changed +5
-0
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,9 @@ test('toAbsoluteLocaleDate', () => {
1818  expect ( toAbsoluteLocaleDate ( '10000-01-01' ,  '' ,  { } ) ) . toEqual ( 'Invalid Date' ) ; 
1919
2020  // test different timezone 
21+   const  oldTZ  =  process . env . TZ ; 
2122  process . env . TZ  =  'America/New_York' ; 
2223  expect ( new  Date ( '2024-03-15' ) . toLocaleString ( ) ) . toEqual ( '3/14/2024, 8:00:00 PM' ) ; 
2324  expect ( toAbsoluteLocaleDate ( '2024-03-15' ) ) . toEqual ( '3/15/2024, 12:00:00 AM' ) ; 
25+   process . env . TZ  =  oldTZ ; 
2426} ) ; 
Original file line number Diff line number Diff line change 11export  function  toAbsoluteLocaleDate ( date : string ,  lang ?: string ,  opts ?: Intl . DateTimeFormatOptions )  { 
22  // only use the date part, it is guaranteed to be in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ) or (YYYY-MM-DD) 
33  // if there is an "Invalid Date" error, there must be something wrong in code and should be fixed. 
4+   // TODO: there is a root problem in backend code: the date "YYYY-MM-DD" is passed to backend without timezone (eg: deadline), 
5+   // then backend parses it in server's timezone and stores the parsed timestamp into database. 
6+   // If the user's timezone is different from the server's, the date might be displayed in the wrong day. 
47  const  dateSep  =  date . indexOf ( 'T' ) ; 
58  date  =  dateSep  ===  - 1  ? date  : date . substring ( 0 ,  dateSep ) ; 
69  return  new  Date ( `${ date }  T00:00:00` ) . toLocaleString ( lang  ||  [ ] ,  opts ) ; 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments