1

I'm developing a Django app I have a model in which timestamps is added automatically. I want to show the time to the end users according to their local timezone.

Here is my code:

settings.py

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

model.py

date_created = models.DateTimeField(auto_now_add=True, null=True)

template

{{object.date_created}}

But it is showing UTC time I want this to detect user's current timezone and render date&time accordingly.

  • You can also use a front-end function to achieve similar results that might be more convenient for you, such as the `Date.now()` function inside javascript. – Turgut Aug 14 '22 at 18:03

1 Answers1

0

The Django framework provides a built-in way to show local time to the user. When Django renders a template, it automatically passes in a variable called {{ localtime }} . This variable contains the current local time in the user's time zone.

If you want to display the local time in a specific format, you can use the Django template filter |date:"FORMAT" . For example, to display the local time in the "long" format, you would use the following code:

{{ localtime|date:"l, F jS Y H:i" }}

This would output something like "Sunday, January 1st 2017 12:00".

Ron Adin
  • 49
  • 8