Help - trying to setFilter using Date

I thought this would be simple, but I can’t figure out why I can’t filter results by date.
I’m just trying to filter the dataset for dates greater than today. This is my code. The database field called ‘date’ is of type “Date and Time”.

var today = new Date();
today = Date.now();
$w(‘#dataset1’).setFilter(wixData.filter()
.contains(‘provinceState’, $w(‘#iState’).value) //ignore this as it works fine
.ge(‘date’, today) //when I add this line, my results are empty whether I use ge, or le
.contains(‘visibility’,‘public’)) //ignore this as it works fine also

Any ideas as to what I’m doing wrong?
thx

One reason could be that I believe that date is a reserved word so naming your field to date is a bad idea. Try to add a new field named dateField as key instead and try again. If that doesn’t do the trick I think you have a different format in your data collection than the format you get from you Date.now() function.

Thanks Andreas. I figured it out. I think there is a bug in wix with the time portion of Date/Time, and if you have a value in the time portion, the call will just fail to return anything. But, when I set the hours to zero, then it worked (using setHours(0,0,0,0)). Also, using Date.now () was ok, but not necessary, as today = new Date(); is sufficient. Working code below:

today = new Date();
today.setHours(0,0,0,0);

$w(’ #dataset1 ‘).setFilter(wixData.filter()
.contains(‘provinceState’, $w(’ #iState ').value)
.ge(‘date’, today)
.contains(‘visibility’,‘public’))

Hey
It is not a bug, when you ask Javascript for a new Date you will get Date and Time in the object and in Wix Data Collection all dates are saved with zero hours, mintes and so on. So the solution is correct.
It is bad though that you cant have just the Date in a field and just the Time in another field.

@hello44 Thanks Andreas. Yes, I understand how the date object works, but it seems that, according to the setFilter API doc (unless I’m missing something), that you should be able to compare two date objects with a LT (less than) or GT (greater than). So if one was “01/01/1980 3:45PM”, and the other was “01/01/2018 12:45PM”, it would be clear which one is greater, but it fails to return no matter if you use GT, or LT - it just won’t compare them. But, if you zero out the time portion, and then compare the date objects using setFilter, then the LT or GT comparison works.

I have been struggling with this exact problem. The fix you suggest @Marcel LeBrun @hello44 does not work. today.setHours(0,0,0,0); gives me an error. “today.setHours is not a function”

@mike70099 have you declared “today” as a Date object? setHours will give you that error if today isn’t a Date. If for instance you did today = Date.Now() then today is likely an integer and that won’t work. You need to use today = new Date();

This explains it further.

@marcellebrun Thank you. Adding “new” fixed it. Silly me. I would have thought that declaring something as a date would, you know, make it a date. :wink: