# query utc time with local time

**URL:** <https://forum.djangoproject.com/t/query-utc-time-with-local-time/8410>\
**Category:** Using the ORM\
**Created:** [June 18, 2021, 8:57pm UTC](https://forum.djangoproject.com/t/query-utc-time-with-local-time/8410 "2021-06-18T20:57:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saarsa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/saarsa/32/4895_2.png) [@saarsa](https://forum.djangoproject.com/u/saarsa)\
**Post date:** [June 18, 2021, 8:57pm UTC](https://forum.djangoproject.com/t/query-utc-time-with-local-time/8410/1 "2021-06-18T20:57:45Z")

</div>

How can I search for time with time zone if I store time by utc  
For the purpose of the example I have inserted datetime ‘2021-06-19T16:20:00+01:00’  
It’s saved as ‘2021-06-19T15:20:00’  
And if I want to search for him I have to search MeteorologicalData.objects.filter(datetime\_\_time=“15:20:00”)  
but if i want to search as MeteorologicalData.objects.filter(datetime\_\_time=“16:20:00+01:00”) It does not return anything

What i can do that he can search for me?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 18, 2021, 11:28pm UTC](https://forum.djangoproject.com/t/query-utc-time-with-local-time/8410/2 "2021-06-18T23:28:43Z")

</div>

Rather than passing the value to the query as a string, convert it to a datetime (or time) object and use that as a parameter.  
See the example in the [Chaining filters](https://docs.djangoproject.com/en/3.2/topics/db/queries/#chaining-filters) section of the docs to get an idea of how to do this.

---

<div class="post-metadata">

**Author:** ![saarsa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/saarsa/32/4895_2.png) [@saarsa](https://forum.djangoproject.com/u/saarsa)\
**Post date:** [June 19, 2021, 12:41am UTC](https://forum.djangoproject.com/t/query-utc-time-with-local-time/8410/3 "2021-06-19T00:41:40Z")

</div>

If I understand correctly it still requires me to change the time to utc in the query?  
To solve it I did that

```auto
def get_or_create(self, *args, **kwargs):
        kwargs["datetime"] = dt.strptime(kwargs["datetime"], r"%Y-%m-%dT%H:%M:%S%z")
        kwargs["datetime"] = kwargs["datetime"].astimezone(pytz.utc)            
        return super().get_or_create(*args, **kwargs)

```

But I’m trying to find a more “django solution”

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 19, 2021, 2:07am UTC](https://forum.djangoproject.com/t/query-utc-time-with-local-time/8410/4 "2021-06-19T02:07:43Z")

</div>

What do you mean by a more “django solution”? Django just provides an abstraction layer on top of the database. It’s the database performing these comparisons, and so you need to supply query data that the database can understand and use.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 21, 2023, 7:29pm UTC](https://forum.djangoproject.com/t/query-utc-time-with-local-time/8410/6 "2023-11-21T19:29:49Z")

</div>

You are responding to a thread that is more than 2 years old with a question that may be about a different issue.

You’re likely to get more of a response if you open a new topic for this, along with providing more details about the problem you’re facing. Include the complete traceback along with any error messages you are receiving.
