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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.