javascript - Update departure date if arrival date is greater -
i want update departure-date in calendar when arrival-date greater, using javascript, adding 1 day. code problem when change arrival-date, departure-date remains same.
function adddate() { date = new date(); var month = date.getmonth() + 1; var day = date.getdate(); var year = date.getfullyear(); if (document.getelementbyid('startdate').value == '') { document.getelementbyid('startdate').value = month + '/' + day + '/' + year; } if (document.getelementbyid('enddate').value == '') { document.getelementbyid('enddate').value = month + '/' + (day + 1) + '/' + year; } }
<input type="text" id="startdate" style="background-color:#5c677b;height:25px;" name="checkin" placeholder="checkin"> <input type="text" id="enddate" style="background-color:#5c677b;height:25px;" name="checkout" placeholder="checkout">
if got right, want to:
- set default current date arrival date , next day departure date
- update departure date when changing arrival date if that's greater
my solution this. firstly have catch event, putting onchange="changeddate()"
. check if arrivaldate>departuredate
, when true date , update departure date next one.
adddate(); function adddate() { date = new date(); var month = date.getmonth() + 1; var day = date.getdate(); var year = date.getfullyear(); if (document.getelementbyid('startdate').value == '') { document.getelementbyid('startdate').value = month + '/' + day + '/' + year; } if (document.getelementbyid('enddate').value == '') { document.getelementbyid('enddate').value = month + '/' + (day + 1) + '/' + year; } } function changeddate(){ var arrivaldate = new date(document.getelementbyid('startdate').value) ; var departuredate = new date(document.getelementbyid('enddate').value) ; if(arrivaldate>departuredate){ var arrdate = new date(); arrdate.setdate(arrivaldate.getdate()+1); arrdate.setmonth(arrivaldate.getmonth()+1); arrdate.setfullyear(arrivaldate.getfullyear()); document.getelementbyid('enddate').value = arrdate.getmonth() + '/' + arrdate.getdate() + '/' + arrdate.getfullyear(); } }
<input type="text" id="startdate" style="background-color:#5c677b;height:25px;" name="checkin" placeholder="checkin" onchange="changeddate()"> <input type="text" id="enddate" style="background-color:#5c677b;height:25px;" name="checkout" placeholder="checkout">
Comments
Post a Comment