@@ -5,29 +5,44 @@ type Granularity = 'hourly' | 'daily' | 'weekly' | 'monthly';
55
66/**
77 * Formats a date to a readable string
8+ * Shows time if the date is within a 24-hour period or spans less than a day
89 */
9- export function formatAnnotationDate ( date : Date | string ) : string {
10- return new Date ( date ) . toLocaleDateString ( 'en-US' , {
11- month : 'short' ,
12- day : 'numeric' ,
13- year : 'numeric' ,
14- } ) ;
10+ export function formatAnnotationDate ( date : Date | string , showTime = false ) : string {
11+ const dateObj = dayjs ( date ) ;
12+ if ( showTime ) {
13+ return dateObj . format ( 'MMM D, h:mm A' ) ;
14+ }
15+ return dateObj . format ( 'MMM D, YYYY' ) ;
1516}
1617
1718/**
1819 * Formats a date range for annotations
20+ * Automatically detects if hourly format is needed based on granularity or date range
1921 */
2022export function formatAnnotationDateRange (
2123 start : Date | string ,
22- end : Date | string | null
24+ end : Date | string | null ,
25+ granularity : Granularity = 'daily'
2326) : string {
24- const startDate = new Date ( start ) ;
25- const endDate = end ? new Date ( end ) : null ;
27+ const startDate = dayjs ( start ) ;
28+ const endDate = end ? dayjs ( end ) : null ;
29+
30+ // If hourly granularity, always show time
31+ const isHourly = granularity === 'hourly' ;
2632
27- if ( ! endDate || startDate . getTime ( ) === endDate . getTime ( ) ) {
28- return formatAnnotationDate ( startDate ) ;
33+ if ( ! endDate || startDate . isSame ( endDate ) ) {
34+ // For single date, show time if hourly or if time is not midnight
35+ const showTime = isHourly || ! startDate . isSame ( startDate . startOf ( 'day' ) ) ;
36+ return formatAnnotationDate ( start , showTime ) ;
2937 }
30- return `${ formatAnnotationDate ( startDate ) } - ${ formatAnnotationDate ( endDate ) } ` ;
38+
39+ // Check if the range spans less than 24 hours or if times differ on same day
40+ const isHourlyRange = startDate . isSame ( endDate , 'day' ) || endDate . diff ( startDate , 'hour' ) < 24 ;
41+
42+ // If hourly granularity or range is within same day, show time
43+ const showTime = isHourly || isHourlyRange || startDate . isSame ( endDate , 'day' ) ;
44+
45+ return `${ formatAnnotationDate ( start , showTime ) } - ${ formatAnnotationDate ( end as Date | string , showTime ) } ` ;
3146}
3247
3348/**
0 commit comments