# How to filter by date field

**URL:** <https://forum.weaviate.io/t/how-to-filter-by-date-field/2618>\
**Category:** Support\
**Created:** [June 5, 2024, 8:39am UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618 "2024-06-05T08:39:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![justin.godden](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@justin.godden](https://forum.weaviate.io/u/justin.godden)\
**Post date:** [June 5, 2024, 8:39am UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/1 "2024-06-05T08:39:20Z")

</div>

Hi, how do I filter searches by date using the python client?

I can see in the docs how to filter by internal datetime objects: [link](https://weaviate.io/developers/weaviate/api/graphql/filters#by-timestamps)

Which mentions you must set `indexTimestamps` to `true` to track these meta timestamps: [Link](https://weaviate.io/developers/weaviate/config-refs/schema#invertedindexconfig--indextimestamps)

But I don’t see anything on how to filter by a user defined timestamp.

I have a field `published_at` defined as such:

```python
...
wc.Property(
        name="published_at",
        data_type=wc.DataType.DATE,
        index_searchable=False,
        skip_vectorization=True,
    ),
...

```

Which is returned in the graphql client as a datetime: `"published_at": "2024-04-22T08:16:47Z",`

In my index I may have some published\_at times that are in the future that I don’t want to retrieve. I would like to filter less than or equal to today’s date (or less than tomorrow’s date).

How is this done using the python client?

Thanks

---

<div class="post-metadata">

**Author:** ![justin.godden](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@justin.godden](https://forum.weaviate.io/u/justin.godden)\
**Post date:** [June 5, 2024, 9:03am UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/2 "2024-06-05T09:03:49Z")

</div>

Solved it using regular property filter and a datetime. For anyone that may find it useful:

```python
def get_tomorrow_midnight() -> datetime:
    now = datetime.now()
    tomorrow = now + timedelta(days=1)
    midnight = datetime(tomorrow.year, tomorrow.month, tomorrow.day)
    return midnight

```

```python
response = collection.query.hybrid(
            ...,
            filters=wq.Filter.by_property("published_at").less_than(
                get_tomorrow_midnight()
            ),
        )

```

May be worth adding something to the docs, as currently the only datetime filtering in the docs is displaying how to filter on weaviate’s meta timestamps, not user-defined ones (unless I missed it).

Cheers

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [June 5, 2024, 11:54am UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/3 "2024-06-05T11:54:10Z")

</div>

hi @justin.godden !

Thanks for sharing and pointing it out.

At the end of the day, the `indexTimestamps` fields are just a date field as published\_at are 🙂

So all date filters will also apply to both meta properties and the ones you define.

Thanks!

---

<div class="post-metadata">

**Author:** ![justin.godden](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@justin.godden](https://forum.weaviate.io/u/justin.godden)\
**Post date:** [June 5, 2024, 12:28pm UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/4 "2024-06-05T12:28:26Z")

</div>

> [@DudaNogueira](#):
>
> So all date filters will also apply to both meta properties and the ones you define.
> 
> Thanks!

Hi Duda,

The only point I was making was the only example of datetime filtering in the docs is using a built-in method specifically for the meta properties - `by_creation_time` - from the first link: `filters=wvc.query.Filter.by_creation_time().greater_or_equal(year2k)`

Unless I missed it, I didn’t see anywhere explaining filtering user defined datetimes. Perhaps it’s implied since you can do value comparison with datetimes already in python (just like using less than for strings works also).

Just my experience that I didn’t find it clear. Up to you if you want to act on that.

Cheers

---

<div class="post-metadata">

**Author:** ![Dirk](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dirk/32/46_2.png) [@Dirk](https://forum.weaviate.io/u/Dirk)\
**Post date:** [June 5, 2024, 1:22pm UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/5 "2024-06-05T13:22:54Z")

</div>

we will add some examples 🙂

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [June 5, 2024, 1:35pm UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/6 "2024-06-05T13:35:56Z")

</div>

Oh! Got it.

It indeed makes sense to have an explicit example there as well as the reference for the meta properties.

Thanks!!

---

<div class="post-metadata">

**Author:** ![SoftwearEnginear](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/softwearenginear/32/1506_2.png) [@SoftwearEnginear](https://forum.weaviate.io/u/SoftwearEnginear)\
**Post date:** [July 12, 2024, 2:29am UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/7 "2024-07-12T02:29:46Z")

</div>

I am having issues trying to filter results by date here.  
I want to filter out documents that are earlier than **2024-07-11** , but somehow documents are not filtered. When I printed the response, I can see dates earlier than 2024-07-11 are still appearing.

Expected retrieved documents should be only be 2024-07-11 or later.

Please let me know if there is a straightforward way to do this:

```auto
import weaviate
from datetime import datetime, timezone, timedelta
from weaviate.classes.query import MetadataQuery, Filter

client = weaviate.connect_to_local()

def format_to_RFC3339_date(date_str):
  date_obj = datetime.striptime(date_str, "%Y-%m-%d")
  offset = timezone(timedelta(hours=8))
  rfc3339_date_with_offset = date_obj.replace(tzinfo=offset).isoformat()
  return rfc3339_date_with_offset

try:
  date_str = "2024-07-11"
  formatted_date = format_to_RFC3339_date(date_str)

  collection = client.collections.get("LlamaIndex")
  response = collection.query.hybrid(
    ...,
    filters=Filter.by_property('creation_date').greater_or_equal(formatted_date),
    return_metadata=MetadataQuery(
      distance=True,
      certainty=True,
      score=True,
      explain_score=True,
    )

  for obj in response.objects:
    print(obj.properties['creation_date']

```

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [July 15, 2024, 5:06pm UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/8 "2024-07-15T17:06:43Z")

</div>

Hi! You should use timestamps.

check here a simple reproducible example:

```python
import weaviate
import os
from weaviate import classes as wvc

client = weaviate.connect_to_local()

client.collections.delete("Collection")
collection = client.collections.create(
    "Collection",
    properties=[
        wvc.config.Property(name="som_date", data_type=wvc.config.DataType.DATE),
        wvc.config.Property(name="some_text", data_type=wvc.config.DataType.TEXT)
    ]
)
collection.data.insert(
    {
        "some_text": "2024-05-05", 
        "some_date": "2024-05-05T23:20:50.52Z"
    }
)
collection.data.insert({
        "some_text": "2024-06-06", 
        "some_date": "2024-06-06T23:20:50.52Z"
    }
)

from weaviate.classes.query import Filter
query = collection.query.fetch_objects(
    filters=Filter.by_property("some_date").greater_or_equal("2024-05-10T23:20:50.52Z")
)
for object in query.objects:
    print(object)

```

Let me know if this helps.  
Thanks!

---

<div class="post-metadata">

**Author:** ![SoftwearEnginear](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/softwearenginear/32/1506_2.png) [@SoftwearEnginear](https://forum.weaviate.io/u/SoftwearEnginear)\
**Post date:** [July 18, 2024, 1:11am UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/9 "2024-07-18T01:11:18Z")

</div>

Hi @DudaNogueira,

I’m currently inserting data using LlamaIndex and I assumed I could use `node.metadata` to filter by dates.

```auto
for obj in response.objects:
     print(obj.properties.keys()) 
# dict_keys([_node_type', 'content', 'page_label', 'last_modified_date', ..., 'creation_date', ...])

```

Is there really no way to use this metadata for filtering? My users have inserted a significant number of documents, and I’d prefer not to delete the collection and ingest the data again.

Thank you!

Edit:  
I have found a solution that actually have to [modify how LlamaIndex queries with Weaviate](https://github.com/run-llama/llama_index/issues/10252).

Although this have resolved my issue with integration, but still does not give me an idea how to query direct with Weaviate using the `node.metadata['last_modified_date] | obj.properties['last_modified_date']`.

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [July 19, 2024, 4:16pm UTC](https://forum.weaviate.io/t/how-to-filter-by-date-field/2618/10 "2024-07-19T16:16:43Z")

</div>

Hi! In order to filter for create or modified date, you need to explicitly set the collection to do so:

> **[Collection schema | Weaviate - Vector Database](https://weaviate.io/developers/weaviate/config-refs/schema#invertedindexconfig--indextimestamps)**
>
> Introduction

Unfortunatelly, when using llamaindex or langchain, it will create the collection for your.

What you can do is to create the collection beforehand, setting all the options you want, and then using llamaindex/langchain.

Also, on that same issue, when creating the collection “outside” of llamaindex/langchain, it is a good idea to set the vectorizer too.

That way you can both use those llm frameworks or query Weaviate directly.

I have crafted a nice recipe here that ilustrates that (using langchain)

[https://github.com/weaviate/recipes/tree/main/integrations/langchain/loading-data](https://github.com/weaviate/recipes/tree/main/integrations/langchain/loading-data)

Let me know if that helps.

Thanks!

Let me know if this helps.
