2

Say I have a model with a field as such:

begin_date = models.DateTimeField(blank=True)

Whenever I want to add a date using a string, for example, '2020-06-15T11:00' it gives me the RuntimeWarning: received a naive datetime while time zone support is active.

So what's the proper way of adding date to a model in Django?

1
  • If you add Z, it is in UTC: '2020-06-15T11:00Z'. Commented Jun 14, 2020 at 22:21

1 Answer 1

3

If you add a 'Z' suffix, you specify the datetime in UTC, for the ISO 8601 standard [Django-doc]:

'2020-06-15T11:00Z'

You can however also pass a datetime object:

from datetime import datetime
from pytz import UTC

datetime(2020, 6, 15, 11, 00, tzinfo=UTC)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.