@@ -13,7 +13,9 @@ type DateTimeProps = {
1313 includeTime ?: boolean ;
1414 showTimezone ?: boolean ;
1515 showTooltip ?: boolean ;
16+ hideDate ?: boolean ;
1617 previousDate ?: Date | string | null ; // Add optional previous date for comparison
18+ hour12 ?: boolean ;
1719} ;
1820
1921export const DateTime = ( {
@@ -23,6 +25,7 @@ export const DateTime = ({
2325 includeTime = true ,
2426 showTimezone = false ,
2527 showTooltip = true ,
28+ hour12 = true ,
2629} : DateTimeProps ) => {
2730 const locales = useLocales ( ) ;
2831 const [ localTimeZone , setLocalTimeZone ] = useState < string > ( "UTC" ) ;
@@ -50,7 +53,8 @@ export const DateTime = ({
5053 timeZone ?? localTimeZone ,
5154 locales ,
5255 includeSeconds ,
53- includeTime
56+ includeTime ,
57+ hour12
5458 ) . replace ( / \s / g, String . fromCharCode ( 32 ) ) }
5559 { showTimezone ? ` (${ timeZone ?? "UTC" } )` : null }
5660 </ Fragment >
@@ -66,7 +70,8 @@ export function formatDateTime(
6670 timeZone : string ,
6771 locales : string [ ] ,
6872 includeSeconds : boolean ,
69- includeTime : boolean
73+ includeTime : boolean ,
74+ hour12 : boolean = true
7075) : string {
7176 return new Intl . DateTimeFormat ( locales , {
7277 year : "numeric" ,
@@ -76,6 +81,7 @@ export function formatDateTime(
7681 minute : includeTime ? "numeric" : undefined ,
7782 second : includeTime && includeSeconds ? "numeric" : undefined ,
7883 timeZone,
84+ hour12,
7985 } ) . format ( date ) ;
8086}
8187
@@ -122,7 +128,7 @@ export function formatDateTimeISO(date: Date, timeZone: string): string {
122128}
123129
124130// New component that only shows date when it changes
125- export const SmartDateTime = ( { date, previousDate = null , timeZone = "UTC" } : DateTimeProps ) => {
131+ export const SmartDateTime = ( { date, previousDate = null , timeZone = "UTC" , hour12 = true } : DateTimeProps ) => {
126132 const locales = useLocales ( ) ;
127133 const realDate = typeof date === "string" ? new Date ( date ) : date ;
128134 const realPrevDate = previousDate
@@ -132,8 +138,8 @@ export const SmartDateTime = ({ date, previousDate = null, timeZone = "UTC" }: D
132138 : null ;
133139
134140 // Initial formatted values
135- const initialTimeOnly = formatTimeOnly ( realDate , timeZone , locales ) ;
136- const initialWithDate = formatSmartDateTime ( realDate , timeZone , locales ) ;
141+ const initialTimeOnly = formatTimeOnly ( realDate , timeZone , locales , hour12 ) ;
142+ const initialWithDate = formatSmartDateTime ( realDate , timeZone , locales , hour12 ) ;
137143
138144 // State for the formatted time
139145 const [ formattedDateTime , setFormattedDateTime ] = useState < string > (
@@ -150,10 +156,10 @@ export const SmartDateTime = ({ date, previousDate = null, timeZone = "UTC" }: D
150156 // Format with appropriate function
151157 setFormattedDateTime (
152158 showDatePart
153- ? formatSmartDateTime ( realDate , userTimeZone , locales )
154- : formatTimeOnly ( realDate , userTimeZone , locales )
159+ ? formatSmartDateTime ( realDate , userTimeZone , locales , hour12 )
160+ : formatTimeOnly ( realDate , userTimeZone , locales , hour12 )
155161 ) ;
156- } , [ locales , realDate , realPrevDate ] ) ;
162+ } , [ locales , realDate , realPrevDate , hour12 ] ) ;
157163
158164 return < Fragment > { formattedDateTime . replace ( / \s / g, String . fromCharCode ( 32 ) ) } </ Fragment > ;
159165} ;
@@ -168,7 +174,7 @@ function isSameDay(date1: Date, date2: Date): boolean {
168174}
169175
170176// Format with date and time
171- function formatSmartDateTime ( date : Date , timeZone : string , locales : string [ ] ) : string {
177+ function formatSmartDateTime ( date : Date , timeZone : string , locales : string [ ] , hour12 : boolean = true ) : string {
172178 return new Intl . DateTimeFormat ( locales , {
173179 month : "short" ,
174180 day : "numeric" ,
@@ -178,18 +184,20 @@ function formatSmartDateTime(date: Date, timeZone: string, locales: string[]): s
178184 timeZone,
179185 // @ts -ignore fractionalSecondDigits works in most modern browsers
180186 fractionalSecondDigits : 3 ,
187+ hour12,
181188 } ) . format ( date ) ;
182189}
183190
184191// Format time only
185- function formatTimeOnly ( date : Date , timeZone : string , locales : string [ ] ) : string {
192+ function formatTimeOnly ( date : Date , timeZone : string , locales : string [ ] , hour12 : boolean = true ) : string {
186193 return new Intl . DateTimeFormat ( locales , {
187- hour : "numeric " ,
194+ hour : "2-digit " ,
188195 minute : "numeric" ,
189196 second : "numeric" ,
190197 timeZone,
191198 // @ts -ignore fractionalSecondDigits works in most modern browsers
192199 fractionalSecondDigits : 3 ,
200+ hour12,
193201 } ) . format ( date ) ;
194202}
195203
@@ -198,6 +206,8 @@ export const DateTimeAccurate = ({
198206 timeZone = "UTC" ,
199207 previousDate = null ,
200208 showTooltip = true ,
209+ hideDate = false ,
210+ hour12 = true ,
201211} : DateTimeProps ) => {
202212 const locales = useLocales ( ) ;
203213 const [ localTimeZone , setLocalTimeZone ] = useState < string > ( "UTC" ) ;
@@ -214,11 +224,13 @@ export const DateTimeAccurate = ({
214224 } , [ ] ) ;
215225
216226 // Smart formatting based on whether date changed
217- const formattedDateTime = realPrevDate
227+ const formattedDateTime = hideDate
228+ ? formatTimeOnly ( realDate , localTimeZone , locales , hour12 )
229+ : realPrevDate
218230 ? isSameDay ( realDate , realPrevDate )
219- ? formatTimeOnly ( realDate , localTimeZone , locales )
220- : formatDateTimeAccurate ( realDate , localTimeZone , locales )
221- : formatDateTimeAccurate ( realDate , localTimeZone , locales ) ;
231+ ? formatTimeOnly ( realDate , localTimeZone , locales , hour12 )
232+ : formatDateTimeAccurate ( realDate , localTimeZone , locales , hour12 )
233+ : formatDateTimeAccurate ( realDate , localTimeZone , locales , hour12 ) ;
222234
223235 if ( ! showTooltip )
224236 return < Fragment > { formattedDateTime . replace ( / \s / g, String . fromCharCode ( 32 ) ) } </ Fragment > ;
@@ -241,7 +253,7 @@ export const DateTimeAccurate = ({
241253 ) ;
242254} ;
243255
244- function formatDateTimeAccurate ( date : Date , timeZone : string , locales : string [ ] ) : string {
256+ function formatDateTimeAccurate ( date : Date , timeZone : string , locales : string [ ] , hour12 : boolean = true ) : string {
245257 const formattedDateTime = new Intl . DateTimeFormat ( locales , {
246258 month : "short" ,
247259 day : "numeric" ,
@@ -251,33 +263,35 @@ function formatDateTimeAccurate(date: Date, timeZone: string, locales: string[])
251263 timeZone,
252264 // @ts -ignore fractionalSecondDigits works in most modern browsers
253265 fractionalSecondDigits : 3 ,
266+ hour12,
254267 } ) . format ( date ) ;
255268
256269 return formattedDateTime ;
257270}
258271
259- export const DateTimeShort = ( { date, timeZone = "UTC" } : DateTimeProps ) => {
272+ export const DateTimeShort = ( { date, timeZone = "UTC" , hour12 = true } : DateTimeProps ) => {
260273 const locales = useLocales ( ) ;
261274 const realDate = typeof date === "string" ? new Date ( date ) : date ;
262- const initialFormattedDateTime = formatDateTimeShort ( realDate , timeZone , locales ) ;
275+ const initialFormattedDateTime = formatDateTimeShort ( realDate , timeZone , locales , hour12 ) ;
263276 const [ formattedDateTime , setFormattedDateTime ] = useState < string > ( initialFormattedDateTime ) ;
264277
265278 useEffect ( ( ) => {
266279 const resolvedOptions = Intl . DateTimeFormat ( ) . resolvedOptions ( ) ;
267- setFormattedDateTime ( formatDateTimeShort ( realDate , resolvedOptions . timeZone , locales ) ) ;
268- } , [ locales , realDate ] ) ;
280+ setFormattedDateTime ( formatDateTimeShort ( realDate , resolvedOptions . timeZone , locales , hour12 ) ) ;
281+ } , [ locales , realDate , hour12 ] ) ;
269282
270283 return < Fragment > { formattedDateTime . replace ( / \s / g, String . fromCharCode ( 32 ) ) } </ Fragment > ;
271284} ;
272285
273- function formatDateTimeShort ( date : Date , timeZone : string , locales : string [ ] ) : string {
286+ function formatDateTimeShort ( date : Date , timeZone : string , locales : string [ ] , hour12 : boolean = true ) : string {
274287 const formattedDateTime = new Intl . DateTimeFormat ( locales , {
275288 hour : "numeric" ,
276289 minute : "numeric" ,
277290 second : "numeric" ,
278291 timeZone,
279292 // @ts -ignore fractionalSecondDigits works in most modern browsers
280293 fractionalSecondDigits : 3 ,
294+ hour12,
281295 } ) . format ( date ) ;
282296
283297 return formattedDateTime ;
0 commit comments