Bootstrap Datepicker

What is a Bootstrap Datepicker?

It is used to display a calendar with the date, month, and time from a dropdown menu.

We will use an example to see how the date pickers are used in bootstrap. The time can be displayed in various formats, and the most frequently used formats are “MM:DD:YYYY”, “DD:MM:YYYY”, “YYYY:MM:DD”, etc.

The default format is, "MM:DD:YYYY”.

Here,

MM:

  • Refers to the months.
  • Can take the values from 01 to 12, i.e. January, February, March, April, May, June, July, August, September, October, November and December.

DD:

  • Refers to the date.
  • Lies between 01 to 31.

YYYY:

  • Refers to the years.
  • For example, 2020, 2002, 2001, 2045 etc.

Usage of date-pickers

  • Selecting interview slots
  • Marking meeting schedules
  • In Time tables
  • Exam Slot bookings
  • Choosing movie/show timings etc.

Example 1:

<!DOCTYPE html>  
<html lang = "en">  
<head>      
  <meta charset = "UTF-8">  
  <meta name = "viewport" content = "width=device-width, initial-scale=1.0">  
  <link rel = "stylesheet" href = "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">  
  <title>Bootstrap DatePicker Example</title>  
  <style>
     body{
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        color: white;
        background-color: black;
     }
     .container{
        margin: auto;
        padding: auto;
     }
     #content{
        align-items: center;
        align-self: center;
        align-content: center;
        text-align: center;
     }
  </style>
</head>  
  
<body>  
   <div class="container">
      <div class="row">
    
          <div class = "col-sm-12 col-lg-12 " id = "content" >   
            <h2>Bootstrap Datepicker Example</h2>  
            <br>
              <p>Following is an example to demonstrate the Bootstrap Datepicker Example:</p><br>
           <input type = "date" name = "date">  
          </div>  
       
   </div>
   </div>
  <script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>  
  <script src ="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
</body>  
</html>  

Output:

Bootstrap Datepicker

We can see the calendar from the dropdown and choose a date accordingly, as shown below,

Bootstrap Datepicker

Example 2:

<!DOCTYPE html>  
<html lang = "en">  
<head> 
   <title>Bootstrap Datepicker Example</title>     
  <meta charset = "UTF-8">  
  <meta name = "viewport" content = "width=device-width, initial-scale=1.0">  
<link rel="stylesheet" href="https://formden.com/static/cdn/bootstrap-iso.css" /> 


<link rel="stylesheet" href="https://formden.com/static/cdn/font-awesome/4.4.0/css/font-awesome.min.css" />


<style>
.bootstrap-iso .formden_header h2, .bootstrap-iso .formden_header p, .bootstrap-iso form{
   font-family: Arial, Helvetica, sans-serif; color: black
   }
.bootstrap-iso form button, .bootstrap-iso form button:hover{
   color: white !important;
   } 
.asteriskField{
   color: red;
   }
body{
   font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
   background-color: thistle;
   color: rgb(39, 29, 29);
}
.form-group{
   padding: 20px;
   margin: 20px;
}
</style>
<body>
<div class="bootstrap-iso">
 <div class="container">
  <div class="row">
   <div class="col-md-12 col-sm-12 col-xs-12">
    <form class="form-horizontal" method="post">
     <div class="form-group ">
      <label class="control-label col-sm-2 requiredField" for="date">
       Set the calender here:
      </label>
      <div class="col-sm-10">
       <div class="input-group">
        <div class="input-group-addon">
         <i class="fa fa-calendar">
         </i>
        </div>
        <input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="text"/>
       </div>
      </div>
     </div>
     <div class="form-group">
      <div class="col-sm-10 col-sm-offset-2">
       <button class="btn btn-warning " name="submit" type="submit">
        Set the Calender
       </button>
      </div>
     </div>
    </form>
   </div>
  </div>
 </div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
<script>
$(document).ready(function(){
var date_input=$('input[name="date"]');
var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
date_input.datepicker({
format: 'mm/dd/yyyy',
container: container,
todayHighlight: true,
autoclose: true,
})
})
</script>
</body>  
</html>

Output:

Bootstrap Datepicker

Click on the input field to access the calendar. As shown below, you can change the date from the dropdown.

Bootstrap Datepicker

Methods that can be used with date-picker

.datepicker

It is used to initialize a datepicker.

remove

It takes no arguments and is used to remove the datepicker. It removes all the attached events, internally attached objects, and added HTML elements.

show

It takes no arguments and is used to show the datepicker.

hide

It takes no arguments and is used to hide the datepicker.

update

It takes no arguments and is used to update the datepicker with the new input value given by the user.

setStartDate

It takes a string for an argument, for example:

startDate (string)

It is used to set a new lower date limit on the datepicker and omit startDate.

It can provide an otherwise false value to unset the limit.

setEndDate

It takes a string for an argument, for example:

endDate (String)

It is used to set a new upper date limit on the datepicker and is used to omit endDate.

It can provide an otherwise false value to unset the limit.

setDaysOfWeekDisabled

It takes a string or an array for an argument, for example:

daysOfWeekDisabled (String|Array)

It is used to set the days of the week that should be disabled and is used to omit daysOfWeekDisabled.
It can provide an otherwise false value to unset the disabled days.


Related Topics

Bootstrap 4 Buttons

Bootstrap Buttons Bootstrap provides many custom button styles. These buttons are used in forms and dialog boxes for actions. Bootstrap offers various predefined classes for providing styles to buttons. There are many classes that are used...

7 minutes read.

Bootstrap 4 Grid Classes

Bootstrap 4 Grid Classes Extra-small Grid – In the extra-small grid system class, when you create the grid (columns), the columns will not be stacked on top of each other on...

8 minutes read.

Bootstrap Typography

Bootstrap provides simple and easily customized typography for headings, body text, lists, and many more. Headline <h1> to <h6> By default, Bootstrap has HTML headings (<h1> to <h6>) that are given below: Example <h1>h1. Bootstrap heading</h1>   <h2>h2. Bootstrap heading</h2>   <h3>h3. Bootstrap heading</h3>   <h4>h4. Bootstrap heading</h4>   <h5>h5. Bootstrap heading</h5>   <h6>h6. Bootstrap heading</h6> Try Now ←...

1 minute read.

Bootstrap Tabs and Pills

Bootstrap Menus Bootstrap menu is used to create different types of menu. It can created horizontal with its .list-inlineclass. <div class="container">       <h3>Example of Bootstrap Menu</h3>       <ul class="list-inline">         <li><a href="#">Home</a></li>         <li><a href="#">About</a></li>         <li><a href="#">Contact</a></li>         <li><a href="#">services</a></li>       </ul>     </div> Try Now Bootstrap Tabs Bootstrap tabl is look like is menu but there is some difference....

4 minutes read.

Bootstrap 4 Tutorial

What is Bootstrap? Bootstrap is a CSS (Cascading Style Sheets) framework, which is used to develop a responsive website. It is an open-source and mobile-first front-end framework. It has design templates that are based...

4 minutes read.

Bootstrap Tables

We can create different types of responsive and awesome bootstrap table like: striped , border etc. Bootstrap Basic Table We can create a basic bootstrap table by using its class .table. It has...

3 minutes read.

Bootstrap With CSS

← Prev Next → ...

1 minute read.

Environment Setup

How to use Bootstrap? We can use bootstrap in our project either by downloading or by providing link of the library files. 1) Download Bootstrap files To download files, visit official site of bootstrap www.getbootstrap.com/download/ and...

1 minute read.

Difference between Bootstrap and WordPress

What is WordPress? WordPress is a free open-source CMS (Content Management System) that is written using Hypertext Pre-processor (PHP) and is paired with MySQL and is supported with HTTPS (Hypertext Transfer...

3 minutes read.

Bootstrap 4 Icons

Bootstrap 4 Icons Bootstrap 3 contains the default icon library. But in bootstrap 4, you have to include the external icon library by yourself. Many free icon libraries are available such...

2 minutes read.

Bootstrap 4 Popover

Bootstrap 4 Popover Popover A popover is similar to the tooltip. The only difference between tooltip and Popover is that in the tooltip, the popup box appears when you move the cursor...

6 minutes read.

Bootstrap 4 Collapse

Bootstrap 4 Collapse Collapse is a bootstrap component that is similar to the dropdown button. Collapse is used to hide or show the content such as paragraphs, sentences, etc.  When we click on a...

4 minutes read.

Bootstrap 4 Scrollspy

Bootstrap 4 Scrollspy Bootstrap 4 offers a scrollspy component that is used to make the website more attractive and eye-catching. Basically, the scrollspy is used on the single-page website. The scrollspy highlights the nav...

5 minutes read.

Difference between bootstrap.css and bootstrap-theme.css

The "bootstrap.css" uses to create a website with pre-existing designs and context. It uses basic Bootstrap classes like rows and columns with a CSS styling tool. The website is styled...

5 minutes read.

Bootstrap Panels

A bootstrap panel is used to create bordered box with some padding around its content. It is created with the .panels classes, and .panel-body classes. There are following classes of the panel: Panel header Panel...

4 minutes read.

Bootstrap 4 Alerts

Bootstrap 4 Alerts Bootstrap 4 offers alerts that are used to provide feedback messages on the user’s actions. There is no boundation of text length in the alerts. Alerts can be created...

1 minute read.

Bootstrap Protocol (BOOTP)

In the following article, we will learn about the Bootstrap Protocol and how it maintains contracts among linked devices on webwork. What is Bootstrap Protocol? The Bootstrap Protocol is a webwork contract...

3 minutes read.

Bootstrap 4 Card

In Bootstrap 4, a card is a bordered box with padding around its content. It has options for headers, footers, colors, content, etc. The card provides the link, title, image,...

8 minutes read.

Bootstrap 4 Spinners

Bootstrap 4 offers spinner is a loading indicator that is used to display the loading state of any component or web page. The spinner is also known as a loader...

7 minutes read.

Bootstrap 4 Filters

 Bootstrap 4 Filters Bootstrap 4 provides a filter component that is used to search an element from a list, table, etc. Bootstrap does not contain any component or utility that helps...

5 minutes read.