@@ -148,8 +148,30 @@ const numericDateWithYearFormatter = new Intl.DateTimeFormat(
148148 numericDateWithYearOptions ,
149149) ;
150150
151- function formatNumericDate ( date : Date , now : Date , timeZone : string | undefined ) : string {
152- const sameYear = date . getFullYear ( ) === now . getFullYear ( ) ;
151+ interface CalendarDay {
152+ year : number ;
153+ /** Days since the epoch, so differences are whole days across DST shifts. */
154+ index : number ;
155+ }
156+
157+ function calendarDay ( date : Date , timeZone : string | undefined ) : CalendarDay {
158+ if ( ! timeZone ) {
159+ const year = date . getFullYear ( ) ;
160+ return { year, index : Date . UTC ( year , date . getMonth ( ) , date . getDate ( ) ) / 86_400_000 } ;
161+ }
162+ const parts = new Intl . DateTimeFormat ( "en-US" , {
163+ timeZone,
164+ year : "numeric" ,
165+ month : "numeric" ,
166+ day : "numeric" ,
167+ } ) . formatToParts ( date ) ;
168+ const part = ( type : Intl . DateTimeFormatPartTypes ) =>
169+ Number ( parts . find ( ( entry ) => entry . type === type ) ?. value ) ;
170+ const year = part ( "year" ) ;
171+ return { year, index : Date . UTC ( year , part ( "month" ) - 1 , part ( "day" ) ) / 86_400_000 } ;
172+ }
173+
174+ function formatNumericDate ( date : Date , sameYear : boolean , timeZone : string | undefined ) : string {
153175 if ( ! timeZone ) {
154176 return ( sameYear ? numericDateFormatter : numericDateWithYearFormatter ) . format ( date ) ;
155177 }
@@ -169,15 +191,13 @@ export function formatDayAwareTimestamp(
169191 if ( ! date ) return "" ;
170192 const time = getTimestampFormatter ( timestampFormat , false , timeZone ) . format ( date ) ;
171193
172- const now = new Date ( nowMs ) ;
173- const startOfToday = new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) ) . getTime ( ) ;
174- const startOfMessageDay = new Date ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) ) . getTime ( ) ;
175- // Round so DST-shifted 23/25 hour days still count as whole days.
176- const dayDiff = Math . round ( ( startOfToday - startOfMessageDay ) / 86_400_000 ) ;
194+ const today = calendarDay ( new Date ( nowMs ) , timeZone ) ;
195+ const messageDay = calendarDay ( date , timeZone ) ;
196+ const dayDiff = today . index - messageDay . index ;
177197
178198 if ( dayDiff <= 0 ) return time ;
179199 if ( dayDiff === 1 ) return `yesterday at ${ time } ` ;
180- return `${ formatNumericDate ( date , now , timeZone ) } ${ time } ` ;
200+ return `${ formatNumericDate ( date , messageDay . year === today . year , timeZone ) } ${ time } ` ;
181201}
182202
183203/** `timeZone` overrides the zone the formatters cached when they were built. */
@@ -191,15 +211,14 @@ export function formatUpcomingTimestamp(
191211 if ( ! date ) return "" ;
192212 const time = getTimestampFormatter ( timestampFormat , false , timeZone ) . format ( date ) ;
193213
194- const now = new Date ( nowMs ) ;
195- const startOfToday = new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) ) . getTime ( ) ;
196- const startOfTargetDay = new Date ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) ) . getTime ( ) ;
197- const dayDiff = Math . round ( ( startOfTargetDay - startOfToday ) / 86_400_000 ) ;
214+ const today = calendarDay ( new Date ( nowMs ) , timeZone ) ;
215+ const targetDay = calendarDay ( date , timeZone ) ;
216+ const dayDiff = targetDay . index - today . index ;
198217
199218 if ( dayDiff < 0 ) return formatDayAwareTimestamp ( isoDate , timestampFormat , nowMs , timeZone ) ;
200219 if ( dayDiff === 0 ) return time ;
201220 if ( dayDiff === 1 ) return `tomorrow at ${ time } ` ;
202- return `${ formatNumericDate ( date , now , timeZone ) } ${ time } ` ;
221+ return `${ formatNumericDate ( date , targetDay . year === today . year , timeZone ) } ${ time } ` ;
203222}
204223
205224type RelativeTimeParts = { value : string ; suffix : string | null } ;
0 commit comments