Skip to content

Commit c6aee7e

Browse files
committed
fix(TimePicker): with some locales, the hour format in the input and picker is inconsistent.
1 parent 6416d3b commit c6aee7e

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

js/src/util/time.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ export const getAmPm = (date, locale) => {
6363
export const formatTimePartials = (values, locale, partial) => {
6464
const date = new Date()
6565

66+
const forceTwoDigit = shouldUseTwoDigitHour(locale)
6667
const formatter = new Intl.DateTimeFormat(locale, {
67-
hour: 'numeric',
68+
hour: forceTwoDigit ? '2-digit' : 'numeric',
6869
minute: '2-digit',
6970
second: '2-digit'
7071
})
@@ -191,3 +192,19 @@ export const isValidTime = time => {
191192
const d = new Date(`1970-01-01 ${time}`)
192193
return d instanceof Date && !Number.isNaN(d.getTime())
193194
}
195+
196+
/**
197+
* Checks whether the given locale formats the hour "9" with a leading zero ("09")
198+
* when using `hour: 'numeric'` in `toLocaleTimeString`.
199+
*
200+
* This helps determine if you should force `hour: '2-digit'` for consistent formatting.
201+
*
202+
* @param {string} locale - The locale code (e.g., "en-US", "pl-PL").
203+
* @returns {boolean} `true` if the formatted hour starts with a leading zero, otherwise `false`.
204+
*/
205+
export const shouldUseTwoDigitHour = locale => {
206+
const d = new Date(2020, 0, 1, 7, 5, 7) // 7:05:07
207+
const formatted = d.toLocaleTimeString(locale)
208+
209+
return formatted.startsWith('0') // check if the hour starts with "0"
210+
}

0 commit comments

Comments
 (0)