Skip to content

Commit 0dd8b60

Browse files
committed
fix(TimePicker): localize minutes and seconds
1 parent fca3762 commit 0dd8b60

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

js/src/time-picker.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
convert24hTo12h,
1717
getAmPm,
1818
getListOfHours,
19-
getMinutesOrSeconds,
19+
getListOfMinutes,
20+
getListOfSeconds,
2021
isAmPm,
2122
isValidTime
2223
} from './util/time'
@@ -347,9 +348,9 @@ class TimePicker extends Picker {
347348
this._timePickerBody.append(
348349
this._createSelect('hours', getListOfHours(this._config.locale)),
349350
timeSeparatorEl.cloneNode(true),
350-
this._createSelect('minutes', getMinutesOrSeconds()),
351+
this._createSelect('minutes', getListOfMinutes(this._config.locale, true)),
351352
timeSeparatorEl,
352-
this._createSelect('seconds', getMinutesOrSeconds())
353+
this._createSelect('seconds', getListOfSeconds(this._config.locale, true))
353354
)
354355

355356
if (isAmPm(this._config.locale)) {
@@ -362,8 +363,8 @@ class TimePicker extends Picker {
362363
_createTimePickerRoll() {
363364
this._timePickerBody.append(
364365
this._createTimePickerRollCol(getListOfHours(this._config.locale), 'hours'),
365-
this._createTimePickerRollCol(getMinutesOrSeconds(), 'minutes'),
366-
this._createTimePickerRollCol(getMinutesOrSeconds(), 'seconds')
366+
this._createTimePickerRollCol(getListOfMinutes(this._config.locale), 'minutes'),
367+
this._createTimePickerRollCol(getListOfSeconds(this._config.locale), 'seconds')
367368
)
368369

369370
if (isAmPm(this._config.locale)) {

js/src/util/time.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,47 @@ export const getListOfHours = locale => Array.from({ length: isAmPm(locale) ? 12
3434
label: (isAmPm(locale) ? i + 1 : i).toLocaleString(locale)
3535
}
3636
})
37-
export const getMinutesOrSeconds = locale => Array.from({ length: 60 }, (_, i) => {
37+
38+
export const getListOfMinutes = (locale, valueAsString = false) => Array.from({ length: 60 }, (_, i) => {
39+
const d = new Date()
40+
d.setMinutes(i)
41+
return {
42+
value: valueAsString ? i.toString() : i,
43+
label: d
44+
.toLocaleTimeString(locale, {
45+
hour: '2-digit',
46+
hour12: false,
47+
minute: '2-digit',
48+
second: '2-digit'
49+
})
50+
.split(':')[1]
51+
}
52+
})
53+
54+
export const getListOfSeconds = (locale, valueAsString = false) => Array.from({ length: 60 }, (_, i) => {
55+
const d = new Date()
56+
d.setSeconds(i)
3857
return {
39-
value: i,
40-
label: i.toLocaleString(locale).padStart(2, (0).toLocaleString(locale))
58+
value: valueAsString ? i.toString() : i,
59+
label: d
60+
.toLocaleTimeString(locale, {
61+
hour: '2-digit',
62+
hour12: false,
63+
minute: '2-digit',
64+
second: '2-digit'
65+
})
66+
.split(':')[2]
4167
}
4268
})
69+
4370
export const getSelectedHour = (date, locale) => date ? (isAmPm(locale) ? convert24hTo12h(date.getHours()) : date.getHours()) : ''
71+
4472
export const getSelectedMinutes = date => (date ? date.getMinutes() : '')
73+
4574
export const getSelectedSeconds = date => (date ? date.getSeconds() : '')
75+
4676
export const isAmPm = locale => ['am', 'AM', 'pm', 'PM'].some(el => new Date().toLocaleString(locale).includes(el))
77+
4778
export const isValidTime = time => {
4879
const d = new Date(`1970-01-01 ${time}`)
4980
return d instanceof Date && d.getTime()

0 commit comments

Comments
 (0)