Hello, I am trying to create a filter that will filter to rows that contain a date between today and a week from today. I came up with the below code but it seems to just filter the dataset to nothing. Any ideas would be very appreciated.
function FIlterToday (){
let Date1 = new Date ();
let Date2 = new Date ( + 7 * 24 * 60 * 60 * 1000 )
$w ( “#dataset1” ). setFilter ( wixData . filter ()
. ge ( ‘fromDate-1’ , Date1 )
. lt ( ‘fromDate-1’ , Date2 )
)
}
Hi Jeff,
You should be able to accomplish this by changing the line:
let Date2 = new Date( + 7 * 24 * 60 * 60 * 1000)
To the following:
let Date2 = new Date( Date1.getTime() + 7 * 24 * 60 * 60 * 1000)
I believe the issue is that the original line is calculating 7 days away from unix epoch (Jan 1, 1970) instead of the current day.
Try this out and let me know if this worked out!
Hi,
I am not an expert but There will be helping you. The code you provided for filtering rows based on a date range between today and a week from today seems almost correct. However, there is a minor error in the way you’re calculating the Date2 variable. The multiplication operation should be outside the parentheses. Here’s the corrected code:
function filterToday() {
let date1 = new Date();
let date2 = new Date(date1.getTime() + 7 * 24 * 60 * 60 * 1000);
$w("#dataset1").setFilter(wixData.filter()
.ge('fromDate-1', date1)
.lt('fromDate-1', date2)
);
}
In this code, we use the getTime() method on date1 to get the timestamp in milliseconds and then add the calculated time for a week (7 days). This ensures that date2 represents a week from today.
Make sure to call the filterToday() function when you want to apply the filter to the dataset. You can trigger it on a button click or any other relevant event.
Additionally, I’ve updated the function name to follow JavaScript naming conventions by starting it with a lowercase letter.