Wix Tutorial - Calculate the number of nights between two datepickers.
Pick two dates in the datepickers and the number of nights in between those dates will be automatically calculated.
Elements:
User Input:
-
datepicker for start date: #datePicker1
-
datepicker for end date: #datePicker2
Element:
-
number of nights: #nights
Wix Code:
$w.onReady( function () {
getDates();
});
export function diff_days() {
let dt1 = new Date($w("#datePicker1").value);
let dt2 = new Date($w("#datePicker2").value);
return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()) ) /(1000 * 60 * 60 * 24));
}
function getDates(){
let diff = diff_days();
$w("#days").text = String(Number(diff));
}
export function datePicker1_change(event, $w) {
let date1 = $w("#datePicker1").value;
getDates();
}
export function datePicker2_change(event, $w) {
let date2 = $w("#datePicker2").value;
getDates();
}
Demo:
Start Date:
End Date:
Number of nights between the two date:
0