Floating date filters in the Python API

Hi All!

Could anyone advise how to replicate filter syntax for floating dates (today, yesterday, tomorrow, etc) through the Python API?

e.g. :

filters = [['created_at', 'is', 'today']]

The format for created_at in the API is a datetime.datetime object with timestamp and TZinfo.
In my use case, I need only the date as otherwise the filter only returns entries created at that instant.

In the UI, I am using query fields and conditions for this as below.

floating_date_filter

Happy to be part of this lovely community.
Thank you!

2 Likes

Hi @sbasu,

Welcome to the forums!

To replicate this in a python script you would use datetime, e.g,

from datetime import date

today = date.today()
print("Today's date:", today)

You can format this to how you need (many docs online about this) and use as a base for having today/tomorrow/yesterday as a known variable.
-David

2 Likes

Thank you David.

Since I need to filter entries created within a 24-hour period daily, I am currently doing this:

date_range = [datetime.today(), datetime.combine(date.today(), datetime.min.time()) - timedelta(0)]

which gives me a timedelta from 12AM today to current time. Following that, I filter out the entries like so:

each_artist_timelog_count_today = len(sg.find('TimeLog', [['created_by', 'is', each_artist], ['created_at', 'between', date_range]], ['created_at', 'created_by'])

Thanks again.