12h/24h Clock Format
Support for both 12-hour and 24-hour clock formats with automatic AM/PM handling
How do I use 12-hour (AM/PM) format?
#12-hour format is the default. It shows hours 1 to 12 with AM and PM buttons, and getValue().time returns a string such as 10:30 AM. Set clock.type to '12h' to be explicit.
The default clock format with AM/PM indicators:
const picker = new TimepickerUI(input, {clock: {type: '12h'}});picker.create();
Features:
- Hours range from 1 to 12
- AM/PM toggle buttons
- Returns time in format: "10:30 AM"
How do I switch to 24-hour format?
#Pass clock: { type: '24h' } to the constructor. Hours then run 0 to 23 with no AM/PM, and getValue().time returns a string such as 14:30.
Military time format without AM/PM:
const picker = new TimepickerUI(input, {clock: {type: '24h'}});picker.create();
Features:
- Hours range from 0 to 23
- No AM/PM indicators
- Returns time in format: "14:30"
How do I follow the browser's 12h or 24h setting?
#Set clock.type to 'auto'. The picker picks 12h or 24h from the browser locale when it is created and each time its options are set. New in 4.5.0.
const picker = new TimepickerUI(input, {clock: {type: 'auto'}});picker.create();
Can I change the clock format at runtime?
#Yes. Call picker.setOptions({ clock: { type: '24h' } }) to switch format after the picker is created. A valid time in the input is rewritten in the new format.
Change clock format dynamically with setOptions():
// Start with 12h formatconst picker = new TimepickerUI(input, {clock: {type: '12h'}});picker.create();// Switch to 24h formatpicker.setOptions({ clock: { type: '24h' } });
Value Format
#12-Hour Format Output
const value = picker.getValue();console.log(value);// Output:{hour: '10',minutes: '30',type: 'AM',time: '10:30 AM',degreesHours: 300, // deprecateddegreesMinutes: 180 // deprecated}
24-Hour Format Output
const value = picker.getValue();console.log(value);// Output:{hour: '14',minutes: '30',type: undefined,time: '14:30',degreesHours: 420, // deprecateddegreesMinutes: 180 // deprecated}
Tip
'auto' to follow the browser.