Set min/max Date of jquery Datepicker - javascript

I am having trouble understanding how to set the min/max Date of the jquery Datepicker. When I check the docs, i see that the dateformat is expected in new Date(2009, 1 - 1, 26).
What does the 1-1 mean? When I chec w3c schools for js dateformats, I cannot find this one.
So I tried
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'yyyy-mm',
minDate: new Date(2001-01),
maxDate: '+15Y'
});
But that leaves me with completely random results... It starts 2006 and ends 2026.
Here is fiddle
http://jsfiddle.net/ANF2y/58/

In JavaScript month is calculated from 0 to 11. So while creating a new Date use current month - 1.
1 - 1 equates to 0. So that is a valid month (0 - January).
In your case set minDate as below.
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
minDate: new Date(2001, 0, 1),
maxDate: '+15Y',
dateFormat: 'yyyy-mm'
});
that is because the jquery date picker only shows 15 items at a time, there is no max date set so you can go as far in the future as you want. When it is set to 2001 you see 2006 in the dropdown as it is limited to 15 items. Select 2006 and then you'll see 2001 in the dropdown. Is your question can I show more items in the year dropdown list?

I'm not going to go into minDate or maxDate as that has been well covered. But in addition to that, what you are looking for is yearRange will set the number of visible items in the year dropdown of the jquery date picker.
The documentation states:
The range of years displayed in the year drop-down: either relative to today's year ("-nn:+nn"), relative to the currently selected year ("c-nn:c+nn"), absolute ("nnnn:nnnn"), or combinations of these formats ("nnnn:-nn"). Note that this option only affects what appears in the drop-down.
Reference

Related

jQuery UI Datepicker - Month and Year Dropdown

I have the following Datepicker on my website.
As you can see from the example they have, it only goes as back as 2006 and as forward as 2026 on the year. I need the year to go back a lot further, how would I accomplish this?
Here is my code:
<script>
$(function() {
$("#passenger_dob").datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd/mm/yy" }).val()
});
</script>
Use the yearRange option to set the years you want do display:
$("#passenger_dob").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "dd/mm/yy",
yearRange: "-90:+00"
});
the yearRange options can be hard-coded, or, as in my example, a range can be used relative to the current date. The options I've used mean "earliest year displayed is 90 years before current year" and "maximum year displayed is equal to current year".
N.B. As noted in the docs, this merely affects the options displayed in the dropdown by default, it doesn't place any restriction on the dates which the user can actually enter/select.
See http://api.jqueryui.com/datepicker/#option-yearRange for more details.

how to set min month at bootstrap datepicker

I have a web application which requires a month/year date to be entered.Currently i am using bootstrap datepicker to show the month of the year.
var minDate = new Date();
minDate.setMonth(minDate.getMonth()-1, 1);
$("#datepicker").datepicker( {
minDate: minDate,
format: "mm-yyyy",
viewMode: "months",
minViewMode: "months"
});
The code above allow the user to select previous month or last month.How can i restrict the user to select previous month or last month by modify the code above?
JSFiddle
You can set a selectable range, using startDate and endDate
For further info, on the available options, check the docs https://bootstrap-datepicker.readthedocs.org/en/latest/options.html

jQuery UI datepicker with custom dynamic date range

I'm using jQuery UI datepicker plugin in one of my application developed with CodeIgniter. And it's working fine they way I expected it to work.
Now I'm working on a new form where I need a custom date range for the datepicker field which user can choose a date from. So far when user comes to the form it's showing a year dropdown field. And once user choose a year from the dropdown my functionality is appending some new fields to the form. So one field is Date Applied, which is a text field and there I'm initializing the jQuery UI datepicker and it's showing the datepicker when user focus on that field.
So this is what I have done so far. Now let me explain about the problem. As per user year selection from very first step I want to restrict Date Applied selection. If user selected 2014 then I want to allow user to choose 1 Oct, 2014 to 30 Sep, 2015. So month range will always 1 Oct - 30 Sep and year will be selected year - selected year + 1.
Some more examples:
2010 - 1 Oct, 2010 to 30 Sept, 2011
2015 - 1 Oct, 2015 to 30 Sept, 2016
2016 - 1 Oct, 2016 to 30 Sept, 2017
Please let me know if anyone can help me how to set these custom dynamic date range in the jQuery UI datepicker.
As in this example on datepicker close event set new minDate and maxDate values.
$("#to").datepicker({
onClose: function(selectedDate) {
$("#from").datepicker("option", "maxDate", selectedDate);
}
});
You can set the year range using this option per documentation here http://api.jqueryui.com/datepicker/#option-yearRange
$( ".selector" ).datepicker({
yearRange: "2002:2012"
});
or
$( ".selector" ).datepicker({
yearRange: "-50:+0"//last 50 year
});
I'm able to make it working. Below is the codes that solve my problem.
var firstYear = userYear;
var endYear = userYear + 1;
$( ".addDatePickerTransfer" ).datepicker({
yearRange: "'" + firstYear + ":" + endYear + "'",
minDate: new Date(firstYear, 10 - 1, 1),
maxDate: new Date(endYear, 9 - 1, 30)
});
NOTE: userYear is the value, I received from user selected year value.

How to validate to allow only current and next date in the date picker using javascript

I have two date pickers one to select the start date and the other for end date.Now I need to validate so that the user can only select current and next date from the date picker and not the other dates.How to validate using javascript
If its about jQuery UI Datepicker, you can provide date selection range as:
$( "#datepicker" ).datepicker({ minDate: 0, maxDate: "+1D" });
In this minDate : 0 represents Today and maxDate: +1D is Tomorrow
Here is Demo for your requirement.

jQuery Date Picker Pick Year First and then Month

What changes are needed to show the Years dropdown first and then the months dropdown as second field in jQuery so users can select which year they are born and then select the month in that year. We still want to restrict that calendar cannot display date greater than today.
If user selects 2011, they should not be allowed to select date greater than today.
I was looking for this also and got here but no solution so I get into jquery-ui.custom.min.js and found the an option. Its as below. Hope it helps although its a bit late..
$( "#datepicker_id" ).datepicker({
showOn: "button",
buttonImage: "images/calendar.png",
buttonImageOnly: true,
dateFormat:'mm-dd-yy',
changeYear: true,
changeMonth: true,
showMonthAfterYear: true, //this is what you are looking for
maxDate:0
});
But if you found that already you should have posted it here :)
For this simple use below line:
startView: 2
And add below line to prevent to select date greater than today:
endDate: new Date(),

Categories

Resources