Strange behaivior of timezone.now()

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

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

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

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

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.

You want to do these comparisons using the proper data types and not as string comparisons. See the docs for date in Field lookup

Also see the first paragraph at Time zones | Django documentation | Django
and the docs for django.utils.timezone.now

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

I understand that it should be

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

and in my case it should be probably

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.

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?

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

If you have a Python datetime object, how do you get the date from it?

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.

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

1 Like

nope (

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

doesn’t fetch todays records

wait! should it be

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

?

No, you’re not comparing character strings.

See the docs referenced above at Strange behaivior of timezone.now() - #2 by KenWhitesell

creation_date__date=timezone.now().date()

?

1 Like

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

damn it! (
)))

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

?

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.)

It was a joke. But I understood.

It works! Thank you for your help, Ken!