timepicker-ui
4.7.0GitHub
timepicker-ui
Features / 12h/24h Format

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:

index.ts
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:

index.ts
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.

index.ts
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():

index.ts
// Start with 12h format
const picker = new TimepickerUI(input, {
clock: {
type: '12h'
}
});
picker.create();
// Switch to 24h format
picker.setOptions({ clock: { type: '24h' } });

Value Format

#

12-Hour Format Output

index.ts
const value = picker.getValue();
console.log(value);
// Output:
{
hour: '10',
minutes: '30',
type: 'AM',
time: '10:30 AM',
degreesHours: 300, // deprecated
degreesMinutes: 180 // deprecated
}

24-Hour Format Output

index.ts
const value = picker.getValue();
console.log(value);
// Output:
{
hour: '14',
minutes: '30',
type: undefined,
time: '14:30',
degreesHours: 420, // deprecated
degreesMinutes: 180 // deprecated
}

Tip

The clock format affects both the visual display and the returned time format. Choose based on your application's locale and user expectations, or use 'auto' to follow the browser.
Esc