Date picker Select Value

Please i have this code that alters the Date Picker and it works fine.
what i want to do now is get the valued Picked on the Start date and End Date, then Minus it to become Number of Nights. Here is the Code so far.

$w.onReady(function () {
	//TODO: write your page related code here...
const startFromDays = 4;
const endAtMonths = 9;
const today = new Date();
let startDate = new Date(today);
let endDate = new Date(today);

startDate.setDate(startDate.getDate() + startFromDays);
endDate.setMonth(endDate.getMonth() + endAtMonths);

$w.onReady(function () {
    $w("#datePicker1").minDate = startDate;
    $w("#datePicker2").maxDate = endDate;
    });	
	});

so how do i get and display to text numberOfNights
Thanks.

let test;
test = endDate - startDate;
console.log(test); //result is 23414400000 which is wrong.

Hi Usman,

The value returned by the DatePicker component is a Javascript Date object which contains the date value in milliseconds. So, it needs to be “convinced” to provide the desired value.

Try using this routine to calculate the difference.

export function date_diff_indays(date1, date2) {
    let dt1 = new Date(date1);
    let dt2 = new Date(date2);
    return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()) ) /(1000 * 60 * 60 * 24));
}

Call the routine like this:

let diff = date_diff_indays(endDate, startDate);

I hope this helps,

Yisrael

i added the code in correspondence to the initial code i posted, here is what happened. its not giving me definite ans.

Where did you add the code?

This is my test code:

$w.onReady(function () {
	//TODO: write your page related code here...
});

export function button1_click(event, $w) {
	//Add your code for this event here: 
	let date1 = $w("#datePicker1").value;
	let date2 = $w("#datePicker2").value;
	console.log(date_diff_indays(date1, date2));
}

export function date_diff_indays(date1, date2) {
    let dt1 = new Date(date1);
    let dt2 = new Date(date2);
    return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()) ) /(1000 * 60 * 60 * 24));
}

My page has two DatePickers (#datePicker1, #datePicker2).

Thank you for Helping me out Yisrael,
I put the code above in Highlight 1
because if i put the code in hightlighted 2, it gives an error that i should be above.

is it ok if i add you as a contributor, wjhat is your email?

i have done it now, like you said. but it is still not working Sir…


it is not consoling.

Please post your URL so I can inspect.

whats your email Address?

We don’t give our our emails (as policy). Please post your URL so I can inspect your page. No one except for authorized Wix personnel have access to your site.

==> Please check to make sure that you do not have more than one onReady() function in your page code. My example that I posted was complete code for a test page, and was not intended to be cut and pasted in its entirety into your code.

Ok. https://infinityproduction7.wixsite.com/wixcoderv/wixcodepage

here is the URL , i wanted to give you access to the backend as a contributor.

You have the button1_click(event, $w) and date_diff_indays(date1, date2) functions inside of the onReady() function which is why you are getting a syntax error. You need to move both of these function outside of the onReady() function.

You should use my example code as a model for what you need to do.

It’s not clear to me what your logic is supposed to be, but in order to progress you need to fix the above problem.

ok, let me see. thanks.

I’ve tried everything Sir, the number of days between eg: 23rd March

and 03April . and so on. ive adjusted the code … i dont know whats going on. i’m starting to lose it. lol.