Hi. I have a site where request a booking for a specific period. The request is written to a database using a custom form. In the form there are two date pickers for the “arrival-date” and the “departure-date”. I want to make a check that the “departure-date” is at least one past the arrival date. I tried to use code for this, but never used it before so in the end I could not work out how to do this. Does anybody already have a similar check build on their site and can show me what the code looks like?
Have you tried doing 'if(departure <= arrival) {do something} on the onBlur of the departure field?
Or another one: the Wix date picker lets you block out dates. So on the departure field, you could block out all dates from arrival date +1
Hi Giri,
I have tried the following:
$w.onReady(function () {
//TODO: write your page related code here…
$w(‘#datePicker1’).onChange(()=>{
let pickerMinDate = $w(“#datePicker1”)+1;
$w(“#datePicker2”).minDate = pickerMinDate;
});
Any help on how to get this working?
PS the site above is a small test website to test this functionality.
Smells like a type conversion. Could you try instead of:
let pickerMinDate = $w(“#datePicker1”)+1;
this
let pickerMinDate = $w(“#datePicker1”).value +1;
Hi Giri, thanks for your help. Just tried your suggestion. Different error message:
It does not pick up the +1. If I do this without +1 it works fine. Any creative thoughts?
Tried to do a set/getDate and then +1? Found this on Stackoverflow:
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result; }
I never did any date transformations, but I believe the problem is here that +1 is ambiguous: +1 what? A millisecond, a second, an hour, a day, a year? So you first have to specify the object (date/day) and then operate on it.
Hi,
I found this nice documentation for comparing two date objects . I found also this Stackoverflow thread regarding the same question.
Good luck!
Tal.
Hi All,
Thank you for your suggestions/help/feedback. I have tried all your suggestions, but I think I am not experienced enough to fully understand it and make it work.
So for now I am going to keep the solution that did work and almost does the trick:
$w(‘#datePicker1’).onChange(()=>{
let pickerMinDate = $w(“#datePicker1”).value;
$w(“#datePicker2”).minDate = pickerMinDate;
});
The above code ensure that the departure date (datepicker2) is at least equal to the arrival date (datepicker1), because all the dates that are before arrival date cannot be selected in the departure date datepicker.