@@ -15,6 +15,7 @@ type DateTimeProps = {
1515 showTooltip ?: boolean ;
1616 hideDate ?: boolean ;
1717 previousDate ?: Date | string | null ; // Add optional previous date for comparison
18+ hour12 ?: boolean ;
1819} ;
1920
2021export const DateTime = ( {
@@ -24,6 +25,7 @@ export const DateTime = ({
2425 includeTime = true ,
2526 showTimezone = false ,
2627 showTooltip = true ,
28+ hour12 = true ,
2729} : DateTimeProps ) => {
2830 const locales = useLocales ( ) ;
2931 const [ localTimeZone , setLocalTimeZone ] = useState < string > ( "UTC" ) ;
@@ -51,7 +53,8 @@ export const DateTime = ({
5153 timeZone ?? localTimeZone ,
5254 locales ,
5355 includeSeconds ,
54- includeTime
56+ includeTime ,
57+ hour12
5558 ) . replace ( / \s / g, String . fromCharCode ( 32 ) ) }
5659 { showTimezone ? ` (${ timeZone ?? "UTC" } )` : null }
5760 </ Fragment >
@@ -67,7 +70,8 @@ export function formatDateTime(
6770 timeZone : string ,
6871 locales : string [ ] ,
6972 includeSeconds : boolean ,
70- includeTime : boolean
73+ includeTime : boolean ,
74+ hour12 : boolean = true
7175) : string {
7276 return new Intl . DateTimeFormat ( locales , {
7377 year : "numeric" ,
@@ -77,7 +81,7 @@ export function formatDateTime(
7781 minute : includeTime ? "numeric" : undefined ,
7882 second : includeTime && includeSeconds ? "numeric" : undefined ,
7983 timeZone,
80- hour12 : false ,
84+ hour12,
8185 } ) . format ( date ) ;
8286}
8387
@@ -124,7 +128,7 @@ export function formatDateTimeISO(date: Date, timeZone: string): string {
124128}
125129
126130// New component that only shows date when it changes
127- export const SmartDateTime = ( { date, previousDate = null , timeZone = "UTC" } : DateTimeProps ) => {
131+ export const SmartDateTime = ( { date, previousDate = null , timeZone = "UTC" , hour12 = true } : DateTimeProps ) => {
128132 const locales = useLocales ( ) ;
129133 const realDate = typeof date === "string" ? new Date ( date ) : date ;
130134 const realPrevDate = previousDate
@@ -134,8 +138,8 @@ export const SmartDateTime = ({ date, previousDate = null, timeZone = "UTC" }: D
134138 : null ;
135139
136140 // Initial formatted values
137- const initialTimeOnly = formatTimeOnly ( realDate , timeZone , locales ) ;
138- const initialWithDate = formatSmartDateTime ( realDate , timeZone , locales ) ;
141+ const initialTimeOnly = formatTimeOnly ( realDate , timeZone , locales , hour12 ) ;
142+ const initialWithDate = formatSmartDateTime ( realDate , timeZone , locales , hour12 ) ;
139143
140144 // State for the formatted time
141145 const [ formattedDateTime , setFormattedDateTime ] = useState < string > (
@@ -152,10 +156,10 @@ export const SmartDateTime = ({ date, previousDate = null, timeZone = "UTC" }: D
152156 // Format with appropriate function
153157 setFormattedDateTime (
154158 showDatePart
155- ? formatSmartDateTime ( realDate , userTimeZone , locales )
156- : formatTimeOnly ( realDate , userTimeZone , locales )
159+ ? formatSmartDateTime ( realDate , userTimeZone , locales , hour12 )
160+ : formatTimeOnly ( realDate , userTimeZone , locales , hour12 )
157161 ) ;
158- } , [ locales , realDate , realPrevDate ] ) ;
162+ } , [ locales , realDate , realPrevDate , hour12 ] ) ;
159163
160164 return < Fragment > { formattedDateTime . replace ( / \s / g, String . fromCharCode ( 32 ) ) } </ Fragment > ;
161165} ;
@@ -170,7 +174,7 @@ function isSameDay(date1: Date, date2: Date): boolean {
170174}
171175
172176// Format with date and time
173- function formatSmartDateTime ( date : Date , timeZone : string , locales : string [ ] ) : string {
177+ function formatSmartDateTime ( date : Date , timeZone : string , locales : string [ ] , hour12 : boolean = true ) : string {
174178 return new Intl . DateTimeFormat ( locales , {
175179 month : "short" ,
176180 day : "numeric" ,
@@ -180,20 +184,20 @@ function formatSmartDateTime(date: Date, timeZone: string, locales: string[]): s
180184 timeZone,
181185 // @ts -ignore fractionalSecondDigits works in most modern browsers
182186 fractionalSecondDigits : 3 ,
183- hour12 : false ,
187+ hour12,
184188 } ) . format ( date ) ;
185189}
186190
187191// Format time only
188- function formatTimeOnly ( date : Date , timeZone : string , locales : string [ ] ) : string {
192+ function formatTimeOnly ( date : Date , timeZone : string , locales : string [ ] , hour12 : boolean = true ) : string {
189193 return new Intl . DateTimeFormat ( locales , {
190194 hour : "2-digit" ,
191195 minute : "numeric" ,
192196 second : "numeric" ,
193197 timeZone,
194198 // @ts -ignore fractionalSecondDigits works in most modern browsers
195199 fractionalSecondDigits : 3 ,
196- hour12 : false ,
200+ hour12,
197201 } ) . format ( date ) ;
198202}
199203
@@ -203,6 +207,7 @@ export const DateTimeAccurate = ({
203207 previousDate = null ,
204208 showTooltip = true ,
205209 hideDate = false ,
210+ hour12 = true ,
206211} : DateTimeProps ) => {
207212 const locales = useLocales ( ) ;
208213 const [ localTimeZone , setLocalTimeZone ] = useState < string > ( "UTC" ) ;
@@ -220,12 +225,12 @@ export const DateTimeAccurate = ({
220225
221226 // Smart formatting based on whether date changed
222227 const formattedDateTime = hideDate
223- ? formatTimeOnly ( realDate , localTimeZone , locales )
228+ ? formatTimeOnly ( realDate , localTimeZone , locales , hour12 )
224229 : realPrevDate
225230 ? isSameDay ( realDate , realPrevDate )
226- ? formatTimeOnly ( realDate , localTimeZone , locales )
227- : formatDateTimeAccurate ( realDate , localTimeZone , locales )
228- : formatDateTimeAccurate ( realDate , localTimeZone , locales ) ;
231+ ? formatTimeOnly ( realDate , localTimeZone , locales , hour12 )
232+ : formatDateTimeAccurate ( realDate , localTimeZone , locales , hour12 )
233+ : formatDateTimeAccurate ( realDate , localTimeZone , locales , hour12 ) ;
229234
230235 if ( ! showTooltip )
231236 return < Fragment > { formattedDateTime . replace ( / \s / g, String . fromCharCode ( 32 ) ) } </ Fragment > ;
@@ -248,7 +253,7 @@ export const DateTimeAccurate = ({
248253 ) ;
249254} ;
250255
251- function formatDateTimeAccurate ( date : Date , timeZone : string , locales : string [ ] ) : string {
256+ function formatDateTimeAccurate ( date : Date , timeZone : string , locales : string [ ] , hour12 : boolean = true ) : string {
252257 const formattedDateTime = new Intl . DateTimeFormat ( locales , {
253258 month : "short" ,
254259 day : "numeric" ,
@@ -258,35 +263,35 @@ function formatDateTimeAccurate(date: Date, timeZone: string, locales: string[])
258263 timeZone,
259264 // @ts -ignore fractionalSecondDigits works in most modern browsers
260265 fractionalSecondDigits : 3 ,
261- hour12 : false ,
266+ hour12,
262267 } ) . format ( date ) ;
263268
264269 return formattedDateTime ;
265270}
266271
267- export const DateTimeShort = ( { date, timeZone = "UTC" } : DateTimeProps ) => {
272+ export const DateTimeShort = ( { date, timeZone = "UTC" , hour12 = true } : DateTimeProps ) => {
268273 const locales = useLocales ( ) ;
269274 const realDate = typeof date === "string" ? new Date ( date ) : date ;
270- const initialFormattedDateTime = formatDateTimeShort ( realDate , timeZone , locales ) ;
275+ const initialFormattedDateTime = formatDateTimeShort ( realDate , timeZone , locales , hour12 ) ;
271276 const [ formattedDateTime , setFormattedDateTime ] = useState < string > ( initialFormattedDateTime ) ;
272277
273278 useEffect ( ( ) => {
274279 const resolvedOptions = Intl . DateTimeFormat ( ) . resolvedOptions ( ) ;
275- setFormattedDateTime ( formatDateTimeShort ( realDate , resolvedOptions . timeZone , locales ) ) ;
276- } , [ locales , realDate ] ) ;
280+ setFormattedDateTime ( formatDateTimeShort ( realDate , resolvedOptions . timeZone , locales , hour12 ) ) ;
281+ } , [ locales , realDate , hour12 ] ) ;
277282
278283 return < Fragment > { formattedDateTime . replace ( / \s / g, String . fromCharCode ( 32 ) ) } </ Fragment > ;
279284} ;
280285
281- function formatDateTimeShort ( date : Date , timeZone : string , locales : string [ ] ) : string {
286+ function formatDateTimeShort ( date : Date , timeZone : string , locales : string [ ] , hour12 : boolean = true ) : string {
282287 const formattedDateTime = new Intl . DateTimeFormat ( locales , {
283288 hour : "numeric" ,
284289 minute : "numeric" ,
285290 second : "numeric" ,
286291 timeZone,
287292 // @ts -ignore fractionalSecondDigits works in most modern browsers
288293 fractionalSecondDigits : 3 ,
289- hour12 : false ,
294+ hour12,
290295 } ) . format ( date ) ;
291296
292297 return formattedDateTime ;
0 commit comments