I am using the bootstrap-datepicker in my site. The datepicker will be used to define search filters. I need the initial value of the datepicker input element to be empty, so it will not introduce the date filter for the search results. When the user clicks on the text input linked to Datepicker, the Datepicker popup box should select the first day of next month.
Problem: If I were to set an initial date (as shown below), the text input will be populated with today's date which is not what I want. However if i do not set the initial date, the datepicker will show 1970s. What should I do?
Similar to: The demo for jQueryUI Datepicker
JS Code
var now = new Date();
var nextMonth = new Date();
nextMonth.setDate(1);
nextMonth.setMonth(now.getMonth() + 1);
var prettyDate = nextMonth.getMonth() + '/' + nextMonth.getDate() + '/' + nextMonth.getFullYear();
$('#listing_filter_available').val(prettyDate);
$('#listing_filter_available').datepicker();
HTML Code
<input type="text" id="listing_filter_available" class="input-small" placeholder="Date Available" />
Failed Attempt
Datepicker does not popup on clicking the text input el
$(function() {
var now = new Date();
var nextMonth = new Date();
nextMonth.setDate(1);
nextMonth.setMonth(now.getMonth() + 1);
var prettyDate = nextMonth.getMonth() + '/' + nextMonth.getDate() + '/' + nextMonth.getFullYear();
$('#listing_filter_available').val(''); // clear the date filter
$('#listing_filter_available').click( function() {
$('#listing_filter_available').val(prettyDate);
$('#listing_filter_available').datepicker();
});
});
You just need to set property valueDefault={null} and the DatePicker will be blank.
In order to set the date format, we only need to add format of one argument and then we will add our required format, which is shown in the following example: Example 1: In this example, we are going to use dd-mm-yyyy format.
Syntax: $(". selector"). datepicker( {defaultDate:"+6"} );
Setting fixed dates If you want to set a fixed date rather than 'x days from now' then you can set this too: var start_date = new Date ('2013-06-21'); var end_date = new Date('2013-12-24'); Which will set the start date to 21 Juen 2103 and the end date to 24 Dec 2013.
There is a much easier way to do this! To set a value
$('#listing_filter_available').datepicker();
$('#listing_filter_available').datepicker('setValue', nextMonth);
To set a blank value
$('#listing_filter_available').datepicker();
$('#listing_filter_available').val('');
I just encountered a very similar use case. With a couple of tweaks to bootstrap-datepicker.js, I believe I've got it working.
You'll need to edit the script in two places.
For the date field, I'm checking to see if the date (this.date) is set to the Unix start date (.e.g blank date). If so, exit and leave the field blank.
(line: 95 - approx)
setValue: function () {
// EDIT - Don't setValue if date = unix date start
if (DPGlobal.formatDate(this.date, this.format) == DPGlobal.formatDate(new Date(1970, 1, 1, 0, 0, 0), this.format)) return false;
// END EDIT
For the drop down calendar, I'm also checking to see if the date (this.date) is set to the Unix start date. If so, set the date to today. I'm doing this so that when a user clicks on the date field, it displays the current month as opposed to Feb 1970.
(line: 144 - approx)
fill: function () {
// EDIT - Set date in fill to today if date is "blank" (eg Unix start date)
var d = ((DPGlobal.formatDate(this.date, this.format) == DPGlobal.formatDate(new Date(1970, 1, 1, 0, 0, 0), this.format))) ? new Date() : new Date(this.viewDate),
// original code
// var d = new Date(this.viewDate),
// END EDIT
I believe that's all you need to do. I've just implemented this in my solution, so if anybody finds any issues or has any better suggestions, I'd love to hear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With