Filter date by month - simply

I want to filter my articles based on their months. There is the getMonth () method, how can I apply it the best?

Here my Try-Code:

wixData.query("myDatabase")
.eq("date".getMonth(), 1)  // is not working
....
...

Have a look at data sort as this can be added to your data query so that you can list your results as ascending or descending etc.

Thanks but I am not looking for sorting.
I just wanna check for equality with the Date-Data.

Well I found a solution… it’s not the best I think, but It works.

wixData.query("myDatabse")
            .ge("date", new Date()) // filter all old Dates out
            .find()
            .then((results) => {
              let filterItems = [];
              for (var i = 0; i < results.length; i++) {
                  const item = results.items[i];

                 for (var j = 0; j < dateMonth.length; j++) {
                     if (item.datum.getMonth() === Number(dateMonth[j])) {      //dateMonth is an Stringarray of numbers between 1 to 12
                            filterItems.push(item._id.toString());
                        }
                    }

                }
                 
                 // then setFilter with filterItems
                
            });

For any other or better solution please show it.

Cheers
Benjamin

1 Like

If it works for you then keep using it :wink: Just check it every so often to make sure that it does still work, in case Wix decide to update something or depreciate something or change anything or certain web browser developers decide to break things again when they update and release a new version pf the browser.

Alternatively, you can add an afterQuery hook:
item.month = item.date.getMonth();

Then on the front end run:

wixData.query("myDatabse")
            .ge("date", new Date()) // filter all old Dates out
            .find()
            .then((results) => {
            let items = results.items;
            items = items.filter(e => e.month = 1);
            })

Another solution, this will obtain all the objects in the month of September

const monthMin = new Date( ‘September 01, 2020’ );
const monthMax = new Date( ‘October 01, 2020’ );

return wixData.query( “MyCollection” )
.descending( “number” ) //optional
.ge( “_dateCreated” , monthMin) //greater or equal than…
.lt( “_dateCreated” , monthMax) //less than…
.find()
.then((results) => {