Robert Chris
Robert Chris

Reputation: 13

Convert datetime field to specific timezone

i have set

TIME_ZONE = 'UTC'

USE_TZ = True

i have a datetime field called "updated_at", i want to covert the value in this field to indian timezone.How can i do that? indian timezone is "Asia/Kolkata"

Upvotes: 1

Views: 432

Answers (2)

Angel F
Angel F

Reputation: 489

You can use pytz to achieve this,

import pytz

datetime_with_new_tz = object.created_at.astimezone(pytz.timezone("Asia/Kolkata"))`

Also, i recommend you that always save datetimes as UTC, then you can convert it in the view or in your template as you need

Ie, if you want to convert the timezone in your template, you can use tz

{% load tz %}
{{object.created_at|timezone:"Asia/Kolkata"}}

Upvotes: 0

nomad
nomad

Reputation: 983

You can change the default timezone.

TIME_ZONE = 'Asia/Kolkata'

Upvotes: 1

Related Questions