stinaq
stinaq

Reputation: 1284

SQLAlchemy find rows between two dates, including the given end date

I want to find all rows between two dates, including the end date, but can only get rows with dates one day before the end date. So for example I have this entry:

{
  "id": 11,
  "taskDate": "Sat, 02 Jan 2016 00:00:00 GMT"
}

And I have this python SQLAlchemy code

tasks = Task.query\
        .filter(Task.task_date >= start_date)\
        .filter(Task.task_date <= end_date)\
        .all()

And if I create end_date from the date '2016-01-02' the row above is not returned. But if I use the date '2016-01-03' it is returned.

To me it should work since I use task_date <= end_date and that should include the given date, but it does not =( I guess I could just add one day to all give end dates, but that seems wrong.

Am I missing something? If I set the start date to a specific date, that is including the one I give.

In my use case I'm only interested in dates, time (hours, minutes, seconds) are irrelevant. I just want the entries that are between two dates. All rows in the database have the normalized time of 00:00:00.

Upvotes: 4

Views: 2239

Answers (1)

stinaq
stinaq

Reputation: 1284

I found the answer, my datetimes in the database was not normalized on microseconds, so the dates were microseconds too large. Now I'm resetting all dates using this:

normalized_date = task_date.replace(hour=0, minute=0, second=0, microsecond=0)

Which makes the code in the question work

Upvotes: 2

Related Questions