Querying datetime fields

I’m having problems filtering dates. Maybe this is because of the format? Or maybe my content needs some reindex on Builder side?

I’m sending eg.:

query.data.dateFrom.$gte: "Fri Jul 17 2020 02:00:00 GMT 0200 (Central European Summer Time)"

and I’m getting results like:

dateFrom: "Wed Jul 15 2020 00:00:00 GMT+0200 (Central European Summer Time)".

(Wed Jul 15 2020… is the actual field value coming from REST API)

Looks like the backend compares strings, not dates :thinking:

Question is: how to query datetime fields properly?

Hi, thanks for reaching out! It seems like you might be trying to target specific content by using $gte. Would you mind providing a little more context around what you’re trying to achieve? Thanks!

Sure!
For simplicity: I have a model eg. myEntry, which has a field date of type Date.
Then I’m creating few myEntry entities with different dates. (API returns values formatted like this: Wed Jul 15 2020 00:00:00 GMT+0200 (Central European Summer Time).)

Then I’m trying to filter entries by my date field: before and after specific date.

For entries with date set BEFORE specific date (or equal to) I was trying:
query.data.date.$lte: [some date toString() or toISOString()],

and for the opposite
query.data.date.$gte: [some date toString() or toISOString()].

With no luck…

Thanks for the details! We’re working on getting an update released soon so that it will be saved as a timestamp (number). That way you can do something like this:

query.data.dateInput.$gte: new Date(...).getTime()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

I’ll follow-up to let you know when it’s been released!

Hi @aubergine! I wanted to let you know that this functionality has been released and you can use the timestamp field type for your query. Please don’t hesitate to reach out if you have additional questions!

:ok_hand: :slight_smile:

Hi,

I’m having the same issue but for the field publishedAt which seems to be saved as a date and not as a timestamp.
Any way to query content depending on the published date ?

Thanks