In model
from django.utils import timezone
timestamp = models.DateTimeField(default=timezone.now)
In View
import pytz
from datetime import datatime
leave = leave.objects.get(pk=1)
time_zone = pytz.timezone('America/Bogota') # set timezone here
Before save
leave.start_date = time_zone.localize(datetime.now())
leave.save()
leave.start_date
> datetime.datetime(2020, 6, 17, 11, 50, 25, tzinfo=<DstTzInfo 'America/Bogota' -05-1 day, 19:00:00 STD>)
# when USE_TZ is False
Using customer’s time zone in template
{% load humanize %}
{% load tz %}
{% timezone user.profile.timezone %}
{% if project.create_date %}{{ project.due_date }} {{user.profile.timezone}} {{ project.due_date|naturaltime }}Not specified {% endif %}
{% endtimezone %}
Timezone
{% load tz %}
{% timezone "Europe/Paris" %}
Paris time: {{ value }}
{% endtimezone %}
{% timezone None %}
Server time: {{ value }}
{% endtimezone %}
Get_current_timezone
{{% load tz %}
The UTC time is {{ object.date }}
{% localtime on %}
The local time is {{ object.date }}
{% endlocaltime %}
Filters
{% load tz %}
{{ value|localtime }}
{{ value|utc }}
{{ value|timezone:"Europe/Paris" }}
Local time
{% load tz %}
{% localtime on %}
{{ value }}
{% endlocaltime %}
{% localtime off %}
{{ value }}
{% endlocaltime %}