Timezone.now() vs datetime.datetime.now()

David542 picture David542 · Nov 15, 2014 · Viewed 10.5k times · Source

When should I be using django's timezone.now() and when should I be using python's datetime.datetime.now().

For example, in the following INSERT which would make more sense?

- Product.objects.create(title='Soap', date_added=datetime.datetime.now())
- Product.objects.create(title='Soap', date_added=timezone.now())

Is there a rule of thumb on when to use each?

Answer

dgel picture dgel · Nov 15, 2014

Just always use timezone.now(). Django now has timezone support which requires timezone 'aware' datetime objects. datetime.now() will return a timezone naive object, whereas timezone.now() will return a timezone aware object.

Read more about Django timezones