Timepicker
Bootstrap 5 Timepicker component
Use MDB custom Timepicker component to select time.
Note: Read the API tab to find all available options and advanced customization
*
*
UMD autoinits are enabled
by default. This means that you don't need to initialize
the component manually. However if you are using MDBootstrap ES format then you should pass
the required components to the initMDB
method.
Basic example
MDB allows you to automatically initialize Timepicker when the page is loaded. To do that you need to
add data-mdb-timepicker-init
attribute in your picker wrapper.
Inline Timepicker with 12h
MDB Timepicker allows for the use of an inline version of the timepicker. The default version is in 12-hour format.
Inline Timepicker with 24h
MDB Timepicker enables you to utilize an inline timepicker with a 24-hour format. To achieve this, set the format24
option to true.
Custom Icon
You can set your custom icon to input. If you will add the icon, the main icon from the input
will be replaced with yours. You have to add class
.timepicker-toggle-button
to your element with data-mdb-toggle
attribute and
.timepicker-icon
to your icon.
With icon
Without icon
MDB Timepicker allows you to configure the input without an icon. You can achieve this by setting
data-mdb-with-icon
attribute to false or using the withIcon
option with a value of false when initializing the
timepicker with JavaScript
Default time
MDB Timepicker allows you to initialize a default time in both the picker and input. You can set it using the
defaultTime
option, which accepts strings like:
- 12:34
- 12:34 PM
- 12:34 AM
and also new Date() format like:
- new Date()
- new Date().toLocaleTimeString([],{timeStyle: 'short'})
Note: If you are using string with PM/AM, you have to have option format24 set to false otherwise your default time will be formate to 24h. Also if you are using string without PM/AM and you would like have 24h format hour you have to set option format24 to true. If you will only put a string without PM/AM and format24 set to false you are gonna have a timepicker with 12h and format set to AM with default.
Handle input value
If you want handle input value upon modal approve, you have to add a listener for the custom event to the selected picker.
Format 24h
The Timepicker allows you to use the 24-hour time format. You can set it using JavaScript options or by using
data-mdb-format24
and setting it to true.
Just Input
You can set the Timepicker to open on input click.
Increment
You can set a increment value by 5 to a minutes.
Max time
Use the max
option to restrict the selection of time beyond the chosen hour.
Max time with PM
Use the max
option to restrict the selection of time beyond the chosen hour.
Max time with AM
Use the max
option to restrict the selection of time beyond the chosen hour.
Min time
Use the max
option to restrict the selection of time before the chosen hour.
Min time with PM
Use the max
option to restrict the selection of time before the chosen hour.
Min time with AM
Use the max
option to restrict the selection of time before the chosen hour.
Disable past
Use the disablePast
option to restrict the selection of time that has already passed.
Disable future
Use the disableFuture
option to restrict the selection of times in the future.
Accessibility
We added proper aria attributes to the timepicker controls to make the component accessible. It's possible to change the values of those attributes by modyfing the component options:
Timepicker - API
Import
Importing components depends on how your application works. If you intend to use the MDBootstrap ES
format, you must
first import the component and then initialize it with the initMDB
method. If you are going to use the UMD
format,
just import the mdb-ui-kit
package.
Usage
Via data attributes
Using the Timepicker component doesn't require any additional JavaScript code - simply add
data-mdb-timepicker-init
attribute to
.form-outline
and use other data attributes to set all options.
For ES
format, you must first import and call the initMDB
method.
Via JavaScript
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to
data-mdb-
, as in data-mdb-body-id=""
.
Name | Type | Default | Description |
---|---|---|---|
animations
|
Boolean | true |
Enables or disables animations for the timepicker. |
bodyId |
String | '' |
Allows to set Timepicker modal body ID. |
cancelLabel |
String | 'Cancel' |
Text of cancel button. |
clearLabel |
String | 'Clear' |
Text of clear button. |
closeModalOnBackdropClick |
Boolean | true |
Sets if a backdrop click will close the picker. |
closeModalOnMinutesClick |
Boolean | false |
Allows to close modal if minutes are selected. |
container |
String | 'body' |
Allows to set the parent element for the timepicker. |
defaultTime |
String | Date | Number | '' |
Allows to initialize a default time in the picker and input. |
disableFuture |
Boolean | false |
Sets constraints to exclude future times from selection. |
disablePast |
Boolean | false |
Sets constraints to exclude times that have already passed from selection. |
disabled |
Boolean | false |
Set a disabled attribute to input in wrapper. |
footerId |
String | '' |
Allows to set Timepicker modal footer ID. |
format12 |
Boolean | true |
Allows to use format 12h. |
format24 |
Boolean | false |
Allows to use format 24h. |
headId |
String | '' |
Allows to set Timepicker modal head ID. |
increment |
Boolean | false |
Allows to set increment minutes by 5. |
inline |
Boolean | false |
Allows to use a inline version of timepicker. |
minTime |
String | Date | null | '' |
Allows to set min time for picker. It accepts minutes and optionally AM/PM format. |
maxTime |
String | Date | null | '' |
Allows to set max time for picker. It accepts minutes and optionally AM/PM format. |
modalId |
String | '' |
Allows to set Timepicker modal ID. |
okLabel |
String | 'Ok' |
Allows to change a text inside submit button. |
overflowHidden |
Boolean | true |
Allows to disable/enable a overflow hidden to body if modal is shown. |
pickerId |
String | '' |
Allows to set Timepicker picker ID. |
readOnly |
Boolean | false |
Allows to set readonly property to input inside wrapper. |
showClearBtn |
Boolean | true |
Allows to attach/remove clear button in modal. |
switchHoursToMinutesOnClick |
Boolean | true |
Allows to enable/disable switching to minutes if hours are selected. |
withIcon |
Boolean | true |
Allows to enable/disable append a icon to input. |
iconClass |
String | 'far fa-clock fa-sm timepicker-icon' |
Allows to set custom classes to timepicker icon. |
pmLabel |
String | 'PM' |
Allows to set custom text to PM button. |
amLabel |
String | 'AM' |
Allows to set custom text to AM button. |
Methods
Name | Description | Example |
---|---|---|
open |
Manually opens a timepicker. |
myTimepicker.open()
|
close |
Manually closes a timepicker. |
myTimepicker.close()
|
dispose
|
Destroy timepicker with this method. |
myTimepicker.dispose()
|
getInstance
|
Static method which allows you to get the timepicker instance associated to a DOM element. |
Timepicker.getInstance(myTimepicker)
|
getOrCreateInstance
|
Static method which returns the timepicker instance associated to a DOM element or create a new one in case it wasn't initialized. |
Timepicker.getOrCreateInstance(myTimepicker)
|
update
|
Updates options of a timepicker instance. |
Timepicker.update({inline: true})
|
Events
Name | Description |
---|---|
clear.mdb.timepicker
|
This event fires when the clear button is clicked. |
valueChanged.mdb.timepicker
|
This event fires immediately when the new input value is set. |
open.mdb.timepicker
|
This event fires immediately when the timepicker is opened. |
close.mdb.timepicker
|
This event fires immediately when the timepicker is closed. |
CSS variables
As part of MDB’s evolving CSS variables approach, timepicker now use local CSS variables for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
Timepicker CSS variables are in different classes which belong to this component. To make it easier to use them, you can find below all of the used CSS variables.