Task : It is necessary to make it so that you can enter a date in the field no less than the day after tomorrow and no more than half a year more , for example, if today is 15.11.2016 , then it should be at least 17.11.2016 (otherwise the date should become 17.11.2016 ) and no more than 15.05.2016 (if more, you need the date to become 15.05.2016 ) .
Problem : If you enter a date less than the day after tomorrow, for example, today (11/15/2016), then everything works, the date becomes two days longer, but if the date is more than +2 days, then it does not work.
Code with crutches, not so long ago I am writing to javascript, I will be glad if someone fixes it.
var myDate = new Date(); // получаем текущую дату №1 var todayDate = new Date(); // получаем текущую дату №2 myDate.setDate(myDate.getDate()+2); // к дате №1 добавляем 2 дня todayDate.setMonth(todayDate.getMonth()+6); // к дате №2 добавляем 6 месяцев var tomorrow = ("0" + (myDate.getDate())).slice(-2) + '.' + ("0" + (myDate.getMonth() + 1)).slice(-2) + '.' + myDate.getFullYear(); // делаем дату №1 вида 31.12.2016 var pieceyear = ("0" + (todayDate.getDate())).slice(-2) + '.' + ("0" + (todayDate.getMonth() + 1)).slice(-2) + '.' + todayDate.getFullYear(); // делаем дату №2 вида 31.12.2016 $('#datepicker').val(tomorrow); // значение поля становится на 2 дня больше сегодняшнего дня function datevalidate() { if($('#datepicker').val() < tomorrow) { // если дата меньше чем послезавтра $('#datepicker').val(tomorrow); // присваиваем дату послезавтра } else if ($('#datepicker').val() > pieceyear) { // если дата больше чем + пол года $('#datepicker').val(pieceyear); // присваиваем дату на пол года больше } } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="datepicker" onchange="datevalidate()">