Bootstrap Datepicker Today Date Selected

Bootstrap Datepicker Today Date Selected
Code Snippet:Bootstrap Datepicker - Set Current Date as Selectet
Author: gearmobile
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 831
License: MIT
Edit Code online: View on CodePen
Read More

This code implements Bootstrap Datepicker with the ability to preselect today’s date. The datepicker is customizable, enabling users to highlight today’s date, set the date format, and control its appearance.

By integrating this code, you effortlessly enhance your web application’s user experience with an intuitive and visually appealing date selection feature.

How to Create Bootstrap Datepicker with Today Date Selected

1. First of all, load the Normalize CSS, Bootstrap, and jQuery by adding the following CDN links into the head tag of your HTML document.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<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-datepicker/1.6.4/css/bootstrap-datepicker3.min.css'>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.min.css'>

<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script> 
<script src='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js'></script>

2. Now, apply the datepicker to your desired HTML elements using their corresponding IDs. In the following code, 'optSimple' is applied to an element with ID 'simple', and 'optComponent' is applied to an element with ID 'datePicker'. You can customize these IDs based on your HTML structure.

<div class="container">
  <div class="row">
		<div class="col-md-12">
			 <form method="post">

				  <!--simple-->
				  <div id="sandbox" class="form-group">
						<label for="simple">Date</label>
						<input id="simple" type="text" class="form-control" value="">
				  </div>

				  <!--component-->
				  <div class="form-group">
						<div class="input-group date" id="datePicker">
							 <input type="text" class="form-control" name="date" value="">
							 <span class="input-group-addon">
								  <i class="glyphicon glyphicon-calendar"></i>
							 </span>
						</div>
				  </div>
				  <button type="submit" class="btn btn-default">Submit</button>
			 </form>
		</div>
  </div>
</div>

<div class="container">
  <div class="row">
		<div class="col-lg-12">
			 <div class="panel">
				  <fieldset>
						<div class="form-group" id="box1">
							 <label for="datepicker1">Date 1</label>
							 <input type="text" class="form-control" id="datepicker1" value="">
						</div>
				  </fieldset>
				  <fieldset>
						<div class="form-group" id="box2">
							 <label for="datepicker2">Date 2</label>
							 <input type="text" class="form-control" id="datepicker2" value="">
						</div>
				  </fieldset>
			 </div>
		</div>
  </div>
</div>

3. Style/customize the date picker using CSS. (Optional)

body {
  margin-top: 40px;
}

.container {
  margin-bottom: 60px;
  border: 1px solid rgba(0, 0, 0, 0.4);
  padding: 40px;
  border-radius: 6px;
}

4. Finally, copy the following JavaScript code into your script file or within <script> tags in your HTML file. This code initializes two datepicker configurations, ‘optSimple’ and ‘optComponent’, catering to different UI preferences.

var date = new Date();
var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());

var optSimple = {
  format: 'mm-dd-yyyy',
  todayHighlight: true,
  orientation: 'bottom right',
  autoclose: true,
  container: '#sandbox'
};

var optComponent = {
  format: 'mm-dd-yyyy',
  container: '#datePicker',
  orientation: 'auto top',
  todayHighlight: true,
  autoclose: true
};

// SIMPLE
$( '#simple' ).datepicker( optSimple );

// COMPONENT
$( '#datePicker' ).datepicker( optComponent );

// ===================================

$( '#datepicker1' ).datepicker({
  format: "mm : dd : yyyy",
  todayHighlight: true,
  autoclose: true,
  container: '#box1',
  orientation: 'top right'
});

$( '#datepicker2' ).datepicker({
  format: 'mm \\ dd \\ yyyy',
  todayHighlight: true,
  autoclose: true,
  container: '#box2',
  orientation: 'top right'
});

$( '#datepicker1, #datepicker2, #simple, #datePicker' ).datepicker( 'setDate', today );

Feel free to tweak the date format, container, and orientation according to your project’s requirements. The code is designed to be easily adaptable, enabling you to enhance the user experience with a feature-rich date selection process.

That’s all! hopefully, you have successfully created Bootstrap Datepicker with today date selected. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X