Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_gte and _lte doesn't work for dates #1528

Open
matronator opened this issue Mar 10, 2024 · 3 comments
Open

_gte and _lte doesn't work for dates #1528

matronator opened this issue Mar 10, 2024 · 3 comments

Comments

@matronator
Copy link

DB file:

{
    "days": [
        {
            "id": 1,
            "date": "2024-03-04T00:00:00",
            "hours": 6,
            "timeRanges": [
                {
                    "id": 1,
                    "dayId": 1,
                    "start": "2024-03-04T08:00:00",
                    "end": "2024-03-04T12:00:00",
                    "image": null
                },
                {
                    "id": 2,
                    "dayId": 1,
                    "start": "2024-03-04T13:00:00",
                    "end": "2024-03-04T15:00:00",
                    "image": null
                }
            ]
        },
        {
            "id": 2,
            "date": "2024-03-05T00:00:00",
            "hours": 8,
            "timeRanges": [
                {
                    "id": 3,
                    "dayId": 2,
                    "start": "2024-03-05T08:00:00",
                    "end": "2024-03-05T12:00:00",
                    "image": null
                },
                {
                    "id": 4,
                    "dayId": 2,
                    "start": "2024-03-05T13:00:00",
                    "end": "2024-03-05T17:00:00",
                    "image": null
                }
            ]
        }
    ]
}

URL: http://localhost:3000/days?date_gte=2024-03-01

Returns an empty array.

@MadhuSaini22
Copy link

@matronator , it's because JSON Server doesn't natively support filtering based on date ranges out of the box.

JSON Server treats query parameters as string values, so when you use _gte, it's comparing the strings lexicographically rather than comparing them as dates. As a result, you're not getting the desired filtering effect.

@matronator
Copy link
Author

Why does this say you can do it for dates?

Any idea how to make it work? Do I have to write some extension or save it as timestamps instead?

@NarciSource
Copy link

It seems to me that lexicographic comparison of strings is not working at all.

db.json
{ "days": [ { "id": 0, "data": "a" }, { "id": 1, "data": "b" }, { "id": 2, "data": "c" }, { "id": 3, "data": "d" }, { "id": 4, "data": "e" }, { "id": 5, "data": "f" }, { "id": 6, "data": "g" } ] }

URL: http://localhost:3000/days?data_gt=c

It is also an empty array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants