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 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 Modal

Bootstrap 4 Modal The Modal is a component of Bootstrap 4, which is a dialog box or pop-up window. The modals are used to provide information or warning to the user, such as...

18 minutes read.

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

4 minutes read.

Bootstrap Glyphicons

Bootstrap Glyphicon is used to create different type of glyphicons It is used in the web project. It provides 260 Glyphicons from the Glyphivons Halflings set. There are various examples of...

1 minute read.

Bootstrap 4 Layout

Containers Containers are the essential elements of the bootstrap layout. It is required when we are using a bootstrap grid system. Containers can also be nested, but most layouts do not need nested containers. Bootstrap has...

3 minutes read.

Bootstrap 4 Dropdowns

Bootstrap Dropdowns Bootstrap 4 provides a dropdown component, which is a toggle and contextual layout, used to display a predefined list of links. It allows the user to select a value from the dropdown...

15 minutes read.

Bootstrap Contact Us page

What is a “Contact Us” page? A “Contact Us” page is a web page available on the websites of various companies, organisations etc., for its users to contact the organisation or...

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.

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 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 4 Grid System

Bootstrap 4 Grid Systems Bootstrap offers a responsive and mobile-first grid system with the help of which we can divide the screen of the web browser into 12 horizontal parts. It has predefined classes...

4 minutes read.

Bootstrap Pager

← Prev Next → ...

1 minute 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 4 Form

Bootstrap 4 Forms Bootstrap 4 provides forms, which are one of the essential components for web pages and web applications. It is an input-based component, used to collect user data with the help of...

8 minutes read.

Bootstrap 4 Progress Bar

Bootstrap Progress Bar Bootstrap 4 offers progress bars that are used to represent the progress of tasks such as downloading a file, software installation, etc. on the computer. It represents the percentage of the...

9 minutes read.

Bootstrap First Example

1) Add HTML5 doctype Before starting the example of Bootstrap, we should know about the some basic rules of Bootstrap. We have to add HTML5 doctype with lang element and CSS properties. <!DOCTYPE html>   <html lang="en">     <head>       <meta charset="utf-8">      </head>   </html> 2) Bootstrap...

2 minutes read.

Bootstrap 4 List Groups

Bootstrap 4 List Groups Bootstrap 4 offers List Group component, which is used to display a series of content in an organized way. The most common list group is an unordered...

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

CakePHP Layouts & Elements

A layout includes a presentation code that can be wrapped out in your view. It means that what you want to see in your view should be well-positioned in layout. When you create...

9 minutes read.

Bootstrap With CSS

← Prev Next → ...

1 minute read.