Let’s say you have this in HTML:
<input type="text" id="date_start">
In javascript we have this:
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var fSDate = new Date(y, m, 1);
var fEDate = new Date(y - 1, m + 1, 0);
$.datepicker.setDefaults({
changeMonth: false,
changeYear: false,
showOtherMonths: false,
yearRange: '-0:+0',
dateFormat: 'yy-mm-dd',
defaultDate: +0, //30 days ago
numberOfMonths: 1,
minDate: fSDate,
maxDate: fEDate,
showAnim: 'fadeIn',
showButtonPanel: false
});
$('#date_start').datepicker();
Pay attention to the fSDate
and fEDate
and notice how you can set the limits for the year, month and day using a little logic. I’ve already subtracted a year from the fEDate
and it will only show at the latest 2016.
Behold this online example in Jsfiddle. This example is great for you to see everything you can change until you get the desired result. Play a little with this Fiddler until you master this one datepicker()
.
You have tried using the Jquery UI datepicker or where you use it you cannot use it? https://jqueryui.com/datepicker/
– user3453562
It is not javascript, but you can manually put: <input type="date" name="bday" max="2016-12-31"> the attribute max
– Gabriel Heguedusch
I’ve thought about it, but I need something more automatic.
– Karina Pinheiro
I already automated in the answer below.
– Gabriel Heguedusch