Hi Friends 👋,
Welcome To aGuideHub! ❤️
In this tutorial, we will learn how to show current date in bootstrap datepicker.
Table of contents
- Includes bootstrap view
- Includes bootstrap library
- Define its class name
- Add JavaScript in
tag
This article will guide you to adding current date in bootstrap datepicker.
Step 1: Includes bootstrap view
To ensure proper rendering and touch zooming for all devices, add the responsive viewport meta tag to your .
meta name="viewport" content="width=device-width, initial-scale=1">
Step 2: Includes bootstrap library
First of all, load the Bootstrap framework CSS into the head tag of your webpage.
script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js">script>
script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">script>
link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.js">script>
link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/css/bootstrap-datepicker.min.css" />
Step 3: Include the code in body
Include the code below in to accept data from the user.
div class="row">
div class="col-lg-2">
div class="panel">
fieldset>
div class="form-group">
label for="datepicker1">Datelabel>
input type="text" class="form-control" id="datepicker1">
div>
fieldset>
div>
div>
div>
Step 4: Add JavaScript in
tag
Add the following JavaScript in tag after the
tag to specify alert auto close.
script>
$(document).ready(function() {
var date = new Date();
var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var end = new Date(date.getFullYear(), date.getMonth(), date.getDate());
$('#datepicker1').datepicker({
format: "mm/dd/yyyy",
todayHighlight: true,
startDate: today,
endDate: end,
autoclose: true
});
$('#datepicker1').datepicker('setDate', today);
});
/script>
Example.
Let’s look at the following example to understand how it basically works:
DOCTYPE html>
html lang="en">
head>
title>Bootstrap Exampletitle>
meta charset="utf-8">
meta name="viewport" content="width=device-width, initial-scale=1">
script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js">script>
script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">script>
link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.js">script>
link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/css/bootstrap-datepicker.min.css" />
head>
body>
div class="row">
div class="col-lg-2">
div class="panel">
fieldset>
div class="form-group">
label for="datepicker1">Datelabel>
input type="text" class="form-control" id="datepicker1">
div>
fieldset>
div>
div>
div>
script>
$(document).ready(function() {
var date = new Date();
var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var end = new Date(date.getFullYear(), date.getMonth(), date.getDate());
$('#datepicker1').datepicker({
format: "mm/dd/yyyy",
todayHighlight: true,
startDate: today,
endDate: end,
autoclose: true
});
$('#datepicker1').datepicker('setDate', today);
});
script>
body>
html>
Check the output of the above code example.
All the best 👍