A Date Picker Value is Returning "null"

Hello!

I’m stuck on trying to get a $w(“#datePicker”) value. All the examples that I’ve seen on the Forum seem pretty straight forward, but I can’t get past line 6 in my code below. Line 6, “purchDate” returns a “null” instead of a date value. Anyone able to clue me in why it’s producing a “null”?

3 $w.onReady( function () {
4 //TODO: write your page related code here…
5
6 let purchDate = $w(" #datePicker2 ").value;
7 let todaysDate = new Date();
8
9 let timeDiff = purchDate.getTime() + todaysDate.getTime();
10
11 let timeConv = Math.ceil(timeDiff / (1000360024));
12
13 console.log (purchDate)

Hi,

As you want this code to run when page ready ( onReady ), the purchDate value will take the initial value, and in your case you left that empty - most likely you left the field with placeholder text -

so, two ways to solve that, First to set todays date as the initial value, Or to create onChange function for that date picker and let all your code inside it.

Hope this helps!
Best,
Mustafa

1 Like

Mustafa,

Thank you. You are absolutely right. It was returning “null” because the Collection’s field was empty.

I thought there was a value there, so I didn’t bother to check or I was checking the wrong record in the collection. In either event, thanks again for pointing me in the right direction.

Have a great week. --Chris