简体   繁体   中英

How do I set as startDate the first day of previous month with Datepicker jquery?

I'm using the jquery datepicker to show a calendar with enabled days only from the first of the previous month and then.I want to set as startDate the first of the previous month with Datepicker jquery. This is the function i use to find the first day of the previous month

function firstOfLastMonth() {
var year = date.getFullYear();
var month = now.setMonth(now.getMonth() -1);
return '01/' + month + '/' + year;      
}

and this is for the datepicker

$('.datech').datepicker({
format: "dd/mm/yyyy",
startDate: firstOfLastMonth,
language: "en"
});

but it doesn't work.

Shouldn't it be 'minDate' instead of 'startDate' assuming you are using the jquery UI datepicker ( docs )? If you want to set the initial value, simply add it to the input field ( <input type="text" class="datech" value="01/01/2018" /> or set the value using the jquery val() method $('.datech').val(firstOfLastMonth())

Change your

return '01/' + month + '/' + year; 

to

return year + '-' + month + '-' + '01'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM