Bootstrap 4 Date Time Picker With jQuery

Bootstrap 4 Date Time Picker With jQuery
File Size: 115 KB
Views Total: 184
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

This is the upgraded version of the smalot's bootstrap-datetimepicker plugin that allows you to create a highly customizable date & time picker in Bootstrap 4 or Bootstrap 3 projects.

Features:

  • Multiple languages.
  • Custom date format.
  • Custom start/end dates.
  • Hour/Day/Month/Year/Decade views.
  • Keyboard navigation.
  • And much more.

How to use it:

1. Load the necessary jQuery library, Bootstrap framework, and Font Awesome iconic font in the document.

<!-- Font Awesome 5 Iconic Font -->
<link rel="stylesheet" href="/path/to/cdn/fontawesome.min.css" />
<!-- jQuery Library -->
<script src="/path/to/cdn/jquery.slim.min.js"></script>
<!-- Bootstrap Framework -->
<link rel="stylesheet" href="/path/to/cdn/bootstrap.min.css" />
<script src="/path/to/cdn/bootstrap.min.js"></script>

2. Download the package and load the following files in the document.

<!-- Stylesheet -->
<link rel="stylesheet" href="/path/to/css/bootstrap-datetimepicker.css" />
<!-- JavaScript -->
<script src="/path/to/js/bootstrap-datetimepicker.js"></script>
<!-- Languages -->
<script src="/path/to/locales/bootstrap-datetimepicker.fr.js"></script>

3. Call the function on the input field to create a default date time picker.

<input type="text" value="2021-04-16 14:45" class="default" />
$(function(){
  $(".default").datetimepicker();
});

4. Customize the date time picker by passing the following options via either JavaScript or data attributes:

$(".default").datetimepicker({

  // parent container
  container: 'body',

  // language
  // you can find all languages under the locales folder
  // attribute: data-date-language
  language: 'en',

  // RTL mode
  rtl: false,

  // step size
  // attributes: data-minute-step
  minuteStep: 5,

  // attributes: data-picker-position
  pickerPosition: 'bottom-right',

  // enable meridian views for day and hour views
  // attributes: data-show-meridian
  showMeridian: false,

  // initial date
  initialDate: new date(),

  // z-index property
  // attributes: data-z-index
  zIndex: undefined,

  // ISO-8601 valid datetime
  format: 'mm/dd/yyyy',

  // 0 = Sunday
  // 6 = Saturday
  // attributes: data-date-weekstart
  weekStart: 0,

  // start/end dates
  // attributes: data-date-startdate and data-date-enddate
  startDate: -Infinity,
  endDate: Infinity,

  // days of the week that should be disabled
  // 0 = Sunday
  // 6 = Saturday
  // attributes: data-date-days-of-week-disabled
  daysOfWeekDisabled: [],

  // auto close the picker after selection
  // attributes: data-date-autoclose
  autoclose: false,

  // auto close the picker after selection
  // attributes: data-date-autoclose
  autoclose: false,

  // 0 or 'hour' for the hour view
  // 1 or 'day' for the day view
  // 2 or 'month' for month view (the default)
  // 3 or 'year' for the 12-month overview
  // 4 or 'decade' for the 10-year overview. 
  // attributes: data-start-view
  startView: 2

  // attributes:data-min-view
  minView: 0,

  // attributes: data-max-view
  maxView: 4,

  // select the view from which the date will be selected
  // 'decade', 'year', 'month', 'day', 'hour'
  // attributes: data-view-select
  viewSelect: 0,

  // show Today button
  // attributes: data-date-today-btn
  todayBtn: false,

  // highlight the current date
  todayHighlight: false,

  // enable keyboard navigation
  // attributes: data-date-keyboard-navigation
  keyboardNavigation: true,

  // whether or not to force parsing of the input value when the picker is closed
  // attributes: data-date-force-parse
  forceParse: false,

});

5. API methods.

// show the datetime picker
$('#myPicker').datetimepicker('show');

// hide the datetime picker
$('#myPicker').datetimepicker('hide');

// update the datetime picker
$('#myPicker').datetimepicker('update');

// destroy the datetime picker
$('#myPicker').datetimepicker('remove');

// set start/end dates
$('#myPicker').datetimepicker('setStartDate', '2020-01-01');
$('#myPicker').datetimepicker('setEndDate', '2030-01-01');

// set the days of week that should be disabled
$('#myPicker').datetimepicker('setDaysOfWeekDisabled', [0,6]);

6. Events.

$('#myPicker').datetimepicker()
.on('changeDate', function(ev){
  //  do something
})
.on('changeMonth', function(ev){
  //  do something
})
.on('changeYear', function(ev){
  //  do something
})
.on('outOfRange', function(ev){
  //  do something
})

Changelog:

v1.0.2 (2021-05-12)

  • JS Updated

This awesome jQuery plugin is developed by simplicitesoftware. For more Advanced Usages, please check the demo page or visit the official website.