Calculate Difference of 2 DatePickers [SOLVED]

I have 2 Date Pickers and a blank text element on the screen

  1. Calculate the difference (number of days) between the 2 date pickers

  2. Multiply the difference by 60 and show in the text element
    This is the code I’m using but its not…

 
$w.onReady( function ()  {
getDates();
});

export function date_diff_indays() {
 let dt1 = new Date($w("#datePicker1"));
 let dt2 = new Date($w("#datePicker2"));
 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 = date_diff_indays();
const rates = (Number(diff)*60);
$w("#price1").text = String(rates);
}

export function datePicker1_change(event, $w) {
 let date1 = $w("#datePicker1").value;
    getDates();
}

export function datePicker2_change(event, $w) {
 let date2 = $w("#datePicker2").value;
    getDates();
}

Hi Shan,

let dt1 = new Date($w("#datePicker1"));
let dt2 = new Date($w("#datePicker2"));

$w(“#datePicker1”) doesn’t resolve in date. You have to access its value property to get the date as you do in the change handlers:

let date1 = $w("#datePicker1").value;

So, the result should be:

let dt1 = new Date($w("#datePicker1").value);
let dt2 = new Date($w("#datePicker2").value);

Thanks again Yevhen :slight_smile:

You’re welcome!