# Strange behaivior of timezone.now()

**URL:** <https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690>\
**Category:** Using the ORM\
**Created:** [November 23, 2024, 10:26am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690 "2024-11-23T10:26:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 23, 2024, 10:26am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/1 "2024-11-23T10:26:44Z")

</div>

In my settings.py I have a Time zone enabled and set it to Ukrainian time.

```auto
TIME_ZONE = 'Europe/Uzhgorod'
USE_I18N = True
USE_TZ = True

```

I’m fetching todays records (today means the same date)

```auto
def index(request):
    mynow = str(timezone.now())
    mydate = mynow[0:10]

    records = Record.objects.filter(user=request.user, creation_date__startswith=mydate).order_by('creation_date')

```

where

```auto
creation_date = models.DateTimeField(auto_now_add=True)

```

It works, but in a strange way. Today after midnight I expected to see an empty results, but it displayed yesterdays records. And this morning It works as expected, showing no records. After I added a record it displays this one (as expected).

It looks like I missed something and timezone doesn’t apply when fetching records.  
Please help.

---

<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 23, 2024, 10:36am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/2 "2024-11-23T10:36:59Z")

</div>

You want to do these comparisons using the proper data types and not as string comparisons. See the docs for [date in Field lookup](https://docs.djangoproject.com/en/5.1/ref/models/querysets/#date)

Also see the first paragraph at [Time zones | Django documentation | Django](https://docs.djangoproject.com/en/5.1/topics/i18n/timezones/#overview)  
and the docs for [django.utils.timezone.now](https://docs.djangoproject.com/en/5.1/ref/utils/#django.utils.timezone.now)

---

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 23, 2024, 10:46am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/3 "2024-11-23T10:46:56Z")

</div>

Thanks for quick reply!  
Yeah, I tried to understand that doc, but failed (

I understand that it should be

```auto
Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))

```

and in my case it should be probably

```auto
Record.objects.filter(user=request.user, creation_date=datetime.date(2005, 1, 1)).order_by('creation_date')

```

however I don’t understand how do I put todays date instead of ‘2005, 1, 1’.

Please help.

---

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 23, 2024, 10:52am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/4 "2024-11-23T10:52:14Z")

</div>

> [@KenWhitesell](#):
>
> Also see the first paragraph at [Time zones | Django documentation | Django](https://docs.djangoproject.com/en/5.1/topics/i18n/timezones/#overview)

oh yeah, that’s probably my problem as Ukraine does use daylight saving time.  
So did I understand correctly that I should switch ‘USE\_TZ’ to False in settings.py as a first step?

---

<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 23, 2024, 10:56am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/5 "2024-11-23T10:56:30Z")

</div>

The function, timezone.now() gives you the current datetime object.

If you have a [Python datetime object](https://docs.python.org/3/library/datetime.html#datetime.datetime.date), how do you get the date from it?

---

<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 23, 2024, 10:57am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/6 "2024-11-23T10:57:35Z")

</div>

> [@VarashDev](#):
>
> So did I understand correctly that I should switch ‘USE\_TZ’ to False in settings.py as a first step?

No, that’s not correct - or at least it doesn’t solve the issue you’re trying to address here.

You’re almost always better off with USE\_TZ being True. You just need to remember that the “date” in a date time field needs to account for that.

---

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 23, 2024, 11:07am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/7 "2024-11-23T11:07:17Z")

</div>

> [@KenWhitesell](#):
>
> If you have a [Python datetime object](https://docs.python.org/3/library/datetime.html#datetime.datetime.date), how do you get the date from it?

probably `my_date = timezone.now().date()` ?

---

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 23, 2024, 11:12am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/8 "2024-11-23T11:12:55Z")

</div>

nope (

```auto
records = Record.objects.filter(user=request.user, creation_date=timezone.now().date()).order_by('creation_date')

```

doesn’t fetch todays records

---

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 23, 2024, 11:15am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/9 "2024-11-23T11:15:40Z")

</div>

wait! should it be

```auto
records = Record.objects.filter(user=request.user, creation_date__startswith=timezone.now().date()).order_by('creation_date')

```

?

---

<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 23, 2024, 11:42am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/10 "2024-11-23T11:42:21Z")

</div>

No, you’re not comparing character strings.

See the docs referenced above at [Strange behaivior of timezone.now() - #2 by KenWhitesell](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/2)

---

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 23, 2024, 11:47am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/11 "2024-11-23T11:47:49Z")

</div>

```auto
creation_date__date=timezone.now().date()

```

?

---

<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 23, 2024, 12:03pm UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/12 "2024-11-23T12:03:05Z")

</div>

Although, you may need to wrap the `timezone.now` call inside the `localize` function.

---

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 23, 2024, 12:14pm UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/13 "2024-11-23T12:14:31Z")

</div>

damn it! (  
)))

```auto
creation_date__date=localize(timezone.now()).date()

```

?

---

<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 23, 2024, 12:16pm UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/14 "2024-11-23T12:16:53Z")

</div>

Sorry, my mistake - it’s `localtime`, not `localize`, as shown in the docs referenced above. (And please, watch the expletives - I understand the frustrations, but we do keep it clean around here.)

---

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 23, 2024, 1:18pm UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/15 "2024-11-23T13:18:08Z")

</div>

It was a joke. But I understood.

---

<div class="post-metadata">

**Author:** ![VarashDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/varashdev/32/24964_2.png) [@VarashDev](https://forum.djangoproject.com/u/VarashDev)\
**Post date:** [November 24, 2024, 5:05am UTC](https://forum.djangoproject.com/t/strange-behaivior-of-timezone-now/36690/16 "2024-11-24T05:05:03Z")

</div>

It works! Thank you for your help, Ken!
