@@ -7,6 +7,16 @@ export interface FormatOptions {
77 timeFormat ?: TimeFormat ;
88}
99
10+ // Server timestamps without an explicit offset (e.g. SQLite-backed responses)
11+ // must be treated as UTC. JS's Date constructor would otherwise parse them as
12+ // local time. Postgres responses include a "Z" suffix and are passed through.
13+ const HAS_TZ_SUFFIX = / [ z Z ] | [ + - ] \d { 2 } : ? \d { 2 } $ / ;
14+
15+ export function parseServerDate ( value : string | Date ) : Date {
16+ if ( value instanceof Date ) return value ;
17+ return new Date ( HAS_TZ_SUFFIX . test ( value ) ? value : `${ value } Z` ) ;
18+ }
19+
1020export function formatDateTime (
1121 dateString : string | null | undefined ,
1222 timezone ?: string ,
@@ -15,7 +25,7 @@ export function formatDateTime(
1525) : string {
1626 if ( ! dateString ) return '-' ;
1727
18- const date = new Date ( dateString ) ;
28+ const date = parseServerDate ( dateString ) ;
1929 if ( Number . isNaN ( date . getTime ( ) ) ) return 'Invalid Date' ;
2030
2131 const hour12 = timeFormat === 'browser' ? undefined : timeFormat === '12h' ;
@@ -150,7 +160,7 @@ export function formatDateTimeEditable(
150160 timeFormat : TimeFormat = 'browser' ,
151161) : string {
152162 if ( ! dateString ) return '' ;
153- const date = new Date ( dateString ) ;
163+ const date = parseServerDate ( dateString ) ;
154164 if ( Number . isNaN ( date . getTime ( ) ) ) return '' ;
155165
156166 const hour12 = timeFormat === 'browser' ? undefined : timeFormat === '12h' ;
0 commit comments