Use MDB custom Timepicker component to select time.
Note: Read the API tab to find all available options and advanced customization
MDB allows us to automatically initialize a Timepicker when the page is loaded.
You have to have a class timepicker in your picker wrapper.
MDB Timepicker allows you to use a inline version timepicker. Default version is with 12h.
const pickerInline = document.querySelector('.timepicker-inline-12');
const timepickerMaxMin = new mdb.Timepicker(pickerInline, { format12:true, inline: true });
MDB Timepicker allows you to use a inline version timepicker. You have to add
options format24 to true.
const pickerInline = document.querySelector('.timepicker-inline-24');
const timepickerMaxMin = new mdb.Timepicker(pickerInline, { format24:true, inline: true });
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 and timepicker-icon to your icon.
MDBTimepicker allows to set input without icon. You can set this with
data-mdb-with-icon set to false or with the options
withIcon set to false if you are initiate timepicker
with js.
const wiTimepicker = document.querySelector('#timepicer-without-icon');
const wiInitTimepicker = new mdb.Timepicker(wiTimepicker, {
withIcon: false
})
MDB Timepicker allows to initialize a default time in the picker and input. You
can set it with option defaultTime. This options accepts strings
like:
and also new Date() format like:
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.
//Default time string PM
const pickerStartedPM = document.querySelector('.timepicker-default-time-string-pm ');
const tmStartedPM = new mdb.Timepicker(pickerStartedPM, {
defaultTime: '02:12 PM'
});
//Default time string AM
const pickerStartedAM = document.querySelector('.timepicker-default-time-string-am');
const tmStartedAM = new mdb.Timepicker(pickerStartedAM, {
defaultTime: '05:12 AM'
});
//Default time without PM/AM
const pickerStartedWithoutPmAm = document.querySelector(
'.timepicker-default-time-string-without-pm-am'
);
const tmStartedWithoutPmAm = new mdb.Timepicker(pickerStartedWithoutPmAm, {
defaultTime: '05:12',
});
//Default time with 24h
const pickerStartedWith24h = document.querySelector('.timepicker-default-time-string-24h');
const tmStartedWith24h = new mdb.Timepicker(pickerStartedWith24h, {
defaultTime: '23:44',
format24: true,
});
//Default time date 12h
const pickerStartedWithDate12h = document.querySelector(
'.timepicker-default-time-with-new-date-12h'
);
const tmStartedWithDate12h = new mdb.Timepicker(pickerStartedWithDate12h, {
defaultTime: new Date(),
});
//Default time date 24h
const pickerStartedWithDate24h = document.querySelector(
'.timepicker-default-time-with-new-date-24h'
);
const tmStartedWithDate24h = new mdb.Timepicker(pickerStartedWithDate24h, {
defaultTime: new Date(),
format24: true,
});
If you want handle input value on modal approve, you have to add custom event to selected picker.
Input value:
const timepickerValue = document.querySelector('#timepicker-value');
const tminputValue = new mdb.Timepicker(timepickerValue);
timepickerValue.addEventListener('input.mdb.timepicker', (input) => {
const valueDiv = document.querySelector('#span-input-value');
valueDiv.innerText = input.target.value;
});
Timepicker allows you to use time format with 24 hours. You can set it with the
JavaScript options or with data-mdb-timepicker-format24 and set it
to true.
const picker = document.querySelector('.timepicker-format');
const tpFormat24 = new mdb.Timepicker(picker, { format24: true });
You can set a timepicker to just an input.
You can set a increment value by 5 to a minutes.
const pickerInc = document.querySelector('.timepicker-inc');
const timepickerInc = new mdb.Timepicker(pickerInc, {
increment: true,
});
You can max time to timepicker with options.
const pickerMaxTime = document.querySelector('.timepicker-max-time');
const timepickerMaxTime = new mdb.Timepicker(pickerMaxTime, {
maxTime: '6:35'
});
You can max time to timepicker with options.
const pickerMaxTimePM = document.querySelector('.timepicker-max-time-pm');
const tmMaxPm = new mdb.Timepicker(pickerMaxTimePM, {
maxTime: '6:35 PM'
});
You can max time to timepicker with options.
const pickerMaxTimeAM = document.querySelector('.timepicker-max-time-am');
const tmMaxAm = new mdb.Timepicker(pickerMaxTimeAM, {
maxTime: '6:35 AM'
});
You can min time to timepicker with options.
const pickerMinTime = document.querySelector('.timepicker-min-time');
const timepickerMinTime = new mdb.Timepicker(pickerMinTime, {
minTime: '10:15'
});
You can min time to timepicker with options.
const pickerMinTimePM = document.querySelector('.timepicker-min-time-pm');
const tmMinPm = new mdb.Timepicker(pickerMinTimePM, {
minTime: '6:35 PM'
});
You can min time to timepicker with options.
const pickerMinTimeAM = document.querySelector('.timepicker-min-time-am');
const tmMinAm = new mdb.Timepicker(pickerMinTimeAM, {
minTime: '6:35 AM'
});
You can max and min hour to timepicker with options.
const pickerMaxMin = document.querySelector('.timepicker-max-min');
const timepickerMaxMin = new mdb.Timepicker(pickerMaxMin, {
maxHour: '12',
minHour: '5'
});
You can set only maximum hour to timepicker.
const pickerMax = document.querySelector('.timepicker-max');
const timepickerMax = new mdb.Timepicker(pickerMax, { maxHour: '5'});
You can set only minimum hour to timepicker.
const pickerMin = document.querySelector('.timepicker-min');
const timepickerMin = new mdb.Timepicker(pickerMin, { minHour: '8'});
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:
amLabel: 'AM',
cancelLabel: 'Cancel',
clearLabel: 'Clear',
invalidLabel: 'Invalid Time Format',
okLabel: 'Ok',
pmLabel: 'PM',
Via data attributes or JavaScript, the timepicker plugin allows to select a time and show it on the selected output.
Add class timpicer to your selected wrapper with input. It
automaticly set timepicker to this element. Timepicker will be toggling with
dynamic icon created in this container.
Add data-mdb-toggle="timepicker" to a input or selected element
like button.
Via JavaScript plugin required a class or ID wrapper element with
timepicker class.
var myTimepicker = new mdb.Timepicker(document.getElementById('wrapper-id'),
options)
Options can be passed via data attributes or JavaScript. For data attributes,
append the option name to data-mdb-timepicker-, as in
data-mdb-timepicker-format24.
| Name | Type | Default | Description |
|---|---|---|---|
| appendValidationInfo | boolean | true |
Allows to append div with information about invalid format |
| bodyID | string | "" |
Allows to set Timepicker modal body ID. |
| cancelLabel | string | "Cancel" |
Text of cancel button. |
| clearLabel | string | "Clear" |
Text of clear button. |
| closeModalOnMinutesClick | boolean | true |
Allows to close modal if minutes are selected. |
| closeOnClickClearBtn | boolean | false |
Allows to close modal if clear button is pressed. |
| disabled | boolean | false |
Set a disabled attribute to input in wrapper |
| enableValidation | boolean | true |
Enables input validation |
| footerID | string | false |
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 |
| invalidLabel | string | "Invalid Time Format" |
Allows to change a text in the invalid div |
| maxHour | string | number | "" |
Allows to set max hour for picker |
| minHour | string | number | "" |
Allows to set min hour for picker |
| 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 | 'fas fas 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 |
| Name | Description | Example |
|---|---|---|
dispose
|
Destroy timepicker with this method |
timepicker.dispose()
|
| Name | Description |
|---|---|
input.mdb.timepicker
|
This event fires when timepicker modal is approved and showing current elements with data-mdb-toggle. You can use it for taking a value when you are saving value to input. |