Bootstrap Time Picker Example

What is a Bootstrap Time picker?

It is used to display the time in hours, minutes and seconds from a dropdown menu. In this section, we will create an example to see how time pickers are used in bootstrap. The time can be displayed in various formats, and the most frequently used format is “HH:MM:SS”.

HH:

  • Refers to the hours.
  • Can lie from 1 to 12(for 12-hour clock).

MM:

  • Refers to the minutes.
  • Lies between 00 to 59.

SS:

  • Refers to the seconds.
  • Lies between 00 to 59

How to implement Time Picker?

First, include all the necessary CDNs in the <head> tag: The bootstrap CDN, the CSS CDN and JQuery CDN. The following CDNs must be included:

<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  
<script src = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>  
<script src = "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>  

Next, include the datetimepicker CDN in the <head> tag.

<script src = "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script>

Now, include any Internal CSS you want to include in the <style> tag. For example,

<style>
.
.
.
</style>

Next, write the main code in the <body> tag, include the time picker component here,

<body>  
<div class ="container">  
    <div class ="row"> 
  .
.
</div>
</div>
</body>

At the end, we can include the JavaScript required for the timepicker in the <script> tag using the .datetimepicker function ,

 <script type = "text/javascript">  
          $(function () {  
              $('#timepicker').datetimepicker({  
                  format: 'LT'  
              });  
          }); 
</script>  

Note: Some methods can also be used in JavaScript in the .datetimepicker function like show, update, remove and hide.

Example 1:

<!DOCTYPE html>
<html>  
<head>  
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  
<script src = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>  
<script src = "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>  
<script src = "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script>  
<title>Bootstrap TimePicker Example</title>  




<script>  
$(function() {  
  $('#datetimepicker1').datetimepicker();  
});  
</script>  
<link rel = "stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">  
<link rel = "stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/css/bootstrap-datetimepicker.min.css">  
<style>  
    body{
      background-color: beige;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }
    .container {  
        margin-top : 80px;  
        width : 800px;  
    }  
</style>  
</head>  
<body>  
</br>  
<div class ="container">  
    <div class ="row">  
       <div class ='col-sm-12 my-auto'>  
          <div class ="form-group">  
            <p>Following is an example to demonstrate timepicker in Bootstrap.</p><br>
             <div class = 'input-group date' id='timepicker'>  
                <input type = 'text' class="form-control" />  
                <span class = "input-group-addon">  
                <span class = "glyphicon glyphicon-time"></span>  
                </span>  
             </div>  
          </div>  
       </div>  
       <script type = "text/javascript">  
          $(function () {  
              $('#timepicker').datetimepicker({  
                  format: 'LT'  
              });  
          });  
       </script>  
    </div>  
 </div>  
</body>  
</html>  

Output:

Bootstrap Time Picker Example

We can click on the “clock” icon to set the time from the dropdown as shown,

Bootstrap Time Picker Example

Example 2:

The following example shows the time in the format “hh:mm:ss”.
<!DOCTYPE html>
<html>
<head>
    <link href= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <script src= "https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <script src= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
    <script type="text/javascript" src= "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
    <link href= "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet">
    <script src= "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
    <title>Bootstrap TimePicker Example</title>  
</head>
<style>  
    body{
      background-color: black;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }
    .para{
        color: white;
    }
    .container {  
        margin-top : 80px;  
        width : 800px;  
    }  
    .content{
        position: relative;
    }
</style> 
<body>
<div class="container">
    <div class="row">
        <p class="para">Following is an example to demonstrate the TimePicker Example using Bootstrap.</p>
        <div class ='col-sm-12 my-auto'> 
<div class="content">
<input class="form-control" type="text" id="datetimepicker" />
</div>
        </div>
</div>
</div>
<script>
    $('#datetimepicker').datetimepicker({
        format: 'hh:mm:ss a'
    });
</script>    
</body>
</html>

Output:

Bootstrap Time Picker Example

We can change the time from the dropdown as shown below,

Bootstrap Time Picker Example

We can even choose a particular value from all the options available,

Bootstrap Time Picker Example

Related Topics

Bootstrap Wells

In Bootstrap, well are used to add a rounded border around an element. It is default some padding and gray background color. It is like a container that displays the content....

1 minute 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 Navigation bar

Navigation Bars In Bootstrap, We can create navigation bar like a navigation header. It is placed at the top of the page. We can collapse or extend it according to screen...

3 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.

Dropdown & Dropup

Bootstrap Dropdowns The bootstrap dropdown menu is used to create a toggleable menu. It provides users to choose one value from a predefined list. Before creating dropdown menu, we have to know...

3 minutes read.

Bootstrap Grid System Example

Bootstrap Grid System Example Stacked to horizontal – In stacked to the horizontal grid system, the grid columns are stacked on top of each other on extra small devices and becomes...

7 minutes read.

Bootstrap Searchable Dropdown Menu

Searchable Dropdown Menu When we click on the dropdown button/arrow in this dropdown menu, an input field will pop up with all the dropdown items listing inside. If we type in...

5 minutes read.

Bootstrap 4 Carousel

Bootstrap 4 Carousel Bootstrap 4 offers a carousel component, which is also known as a slider or slideshow. The carousel is used for the dynamic representation of content using images with...

7 minutes read.

Bootstrap Badges and Labels

Bootstrap Badges Bootstrap Badges are numerical indicators. It is used to show that how many items are associated with a link.The .badge class is used to create of Bootstrap Badges. Let us see...

2 minutes read.

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 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 Multiselect Dropdown

What is a multi-select Dropdown? Multi-select dropdown lists are used when we want to select multiple options for a particular record. Usually, dropdown lists provide the feature of choosing just one value...

4 minutes 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 Toast Example

What are toasts? Toasts are similar to notifications, and they are lightweight and act as push notifications from mobile and desktop operating systems. They’re built using flexbox in Bootstrap. They can...

4 minutes read.

Text Shadows in CSS

Text shadows are created with the text-shadow CSS property. It takes a list of shadows separated by commas to be applied to the text and any of its styles. Each...

5 minutes read.

Bootstrap 4 Jumbotron

Bootstrap 4 Jumbotron Bootstrap offers Jumbotron, which is used to get more attention to specific content or information on the web page. It provides a gray background-color to the content to make that content...

2 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 4 Toast

Bootstrap 4 Toast Bootstrap 4 offers a Toast component, which is quite similar to the alert box and pushes notifications. The toast is visible on the screen for a few seconds when any...

5 minutes 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 Images

Bootstrap 4 Images Bootstrap provides several classes for images to make them responsive and also helps to give some style to the images. Image Shapes Rounded Corners Bootstrap 4 offers .rounded class that makes the corners...

5 minutes read.