Filtering Dataset with today's date? No luck

Hi there,

Am trying to get up to speed on using page code to perform what should be a pretty easy task I thought, but failing.

Any help would be greatly appreciated!


I am trying to connect a repeater to elements from a dataset that is filtered by a “Date” field that I would like to match today’s date.

I have so far:

  1. Used a call to get the date, formatted it, and assigned it to a textid so I can see what is generated, e.g., “Jul 25”.

I would then like to filter my dataset to show only those entries which have “Jul 25” in the date field.

===
Right now my filtering attempts either yield nothing or bizarre matches and I have tried multiple ways on the

.contains item

I have even tried putting .contains(“Date”,“Jul 25”));

and still don’t get the expected matches.

My database from which the dataset is generated has a text field in which I put date strings of that format.

Here’s the latest incarnation (which doesn’t yield anything).

// For full API documentation, including code examples, visit Velo API Reference - Wix.com

import wixData from ‘wix-data’;

$w.onReady( function () {
// Gets today’s date
const today = new Date();
// Sets the property of the text element to be a string representing today’s date in the user’s local format
const options = {
day: “numeric”,
month: “short”

};

$w(“#text36”).text = today.toLocaleDateString(“en-US”,options);

$w(“#dataset2”).setFilter(wixData.filter()
.contains(“Date”,“#text36”));

});

Hi Deborah,

The code above filters for records where the “Date” field contains the text “#text36”, not the content of the text element with that ID.

You can try this:

const todayStr = today.toLocaleDateString("en-US",options);
$w("#dataset2").setFilter(wixData.filter().contains("Date", todayStr));

If that doesn’t work out, please share a link to your site so we might have a closer look.

I’m trying to get this to work…using today’s date in filtering a dataset, but am running into issues.


I have a dataset on the page named: dataset2
My column to filter is named (this is a string field): TheDate

Console log shows the correct date from my script.
How do I get dataset2 to use the date string created from today’s date to filter the dataset?

I get this error in the console: $w(…).setFilter is not a function

Here is my code so far - (I’ve hard coded the filter date for testing):

import wixData from “wix-data”;

var d = new Date();
var todayStr = (d.getMonth()+1) + “/” + d.getDate() + “/” + d.getFullYear();
console.log(todayStr);

$w(“#dataset2”).setFilter(wixData.filter().contains(“TheDate”, ‘4/9/2020’));

Perhaps you need to wrap the filtering code to be inside a $w.onReady block - until then the page components (including the dataset) are not available

Thanks Ofer, error is gone, but it’s still not filtering the data.

import wixData from “wix-data”;

var d = new Date();
var todayStr = (d.getMonth()+1) + “/” + d.getDate() + “/” + d.getFullYear();
console.log(todayStr);

$w.onReady( function () {

$w(“#dataset2”).setFilter(wixData.filter().contains(“TheDate”, ‘4/9/2020’));

});

Can you share a link to your site?

https://pointheremedia.wixsite.com/mysite

The button that shows WEDNESDAY is pulling the dynamic content. Based on my filter, it should be pulling THURSDAY.

There’s an error with this code (visible in the developer tools console):

$w("#dataset2").setFilter(wixData.filter().contains("sort", 2));

The error is:

Uncaught (in promise) Error: Failed to build a filter.
WDE0044: Invalid .contains parameter value [Number]. .contains parameter must be a String..

I see the code currently doesn’t use “todayStr” at all, and filters on the “sort” field - maybe you changed after posting the question?
Anyway, to use the “sort” field, change the operator from “contains” to “eq”

Ok…getting closer! I was changing things up to try to get something working.

This works:
$w(“#dataset2”).setFilter(wixData.filter().eq(“sort”,2));

but using .contains does not work, not with the number or text string fields.

.eq doesn’t work with the string fields at all.

Should work…
“eq” for full equality and “contains” for substrings (in string fields).
Now that I think of it, maybe the “/” character could be problematic… not sure though

I tried just a string with the letter ‘t’ and it didn’t work either.

Strange thing. I created a new text field and now it works just fine. Thank you so much for the guidance and help! Much appreciated.

Here’s my completed code for anyone who wants to use it. Filters the dataset on the page for today’s date. Could be used for many different things, such as posting the School Day, etc.

Dataset on page named: dataset2

Column to filter names: yikes

import wixData from “wix-data”;

var d = new Date();
var todayStr = (d.getMonth()+1) + “/” + d.getDate() + “/” + d.getFullYear();
console.log(todayStr);

$w.onReady( function () {

$w(“#dataset2”).setFilter(wixData.filter().contains(“yikes”, todayStr));
console.log($w(“#dataset2”));

});

Hello
I’ve tryed your code replacing dataset name (dataset 3) and Column names (finLocation) but it doesn’t work and I don’t know why. I use a date field in my collection…could it be the mistake ?