Open In App

How to use Bootstrap Datepicker to get date on change event ?

In this article, we will learn how to use Bootstrap Datepicker to get a date on changes of events. Bootstrap Datepicker is an open-source repository that provides an API to integrate date time picker into the frontend of the website. One should have a basic knowledge of HTML, CSS, jQuery, and Bootstrap.

Approach:



<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css” />
<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.css” />
<link rel=”stylesheet” href=”https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css” />   

<script src=”https://code.jquery.com/jquery-3.3.1.slim.min.js”> </script>
<script src= “https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js”></script>
<script src=”https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js”></script>



Example: In this example, we take an input field with class name as “datepicker”, and initialize the datepicker on this input using datepicker()

A calendar appears when the user clicks the input field, and a date can be selected from the calendar. As soon as the user selects a date from the calendar, it is automatically written into the input field. We add an event listener onchange for that input field. When a selection is made in that input field, the onchange() callback method is automatically called to get the value of the input field using the jQuery val() method.  




<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet"
          href=
    <link rel="stylesheet" 
          href=
    <link rel="stylesheet" 
          href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
  
    <style>
        .container {
            display: flex;
            justify-content: center;
            margin-top: 30px;
        }
      
        h1,
        container {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1 class="container">
        GeeksforGeeks
    </h1>
    <div class="container">
        <div class="datepicker date 
                    input-group p-0 shadow-sm">
            <input id="reservationDate" 
                   type="text"
                   placeholder="Choose a date" 
                   class="form-control py-4 px-4" />
            <div class="input-group-append"
              <span class="input-group-text px-4">
                  <i class="fa fa-clock-o"></i>
                </span
            </div>
        </div>
    </div>
    <div class="container">
        <p id="showdate"> No Date is Picked </p>
    </div>
  
    <script>
        $(".datepicker").datepicker({
            clearBtn: true,
            format: "dd/mm/yyyy",
        });
        $(".datepicker").on("change", function() {
            let pickedDate = $("input").val();
            $("#showdate").text(
            `You picked this ${pickedDate} Date`);
        });
    </script>
</body>
  
</html>

Output:


Article Tags :