How to use TimeZone in Django

52 Views Asked by At

In my django project use:

TIME_ZONE = 'Asia/Ho_Chi_Minh'
USE_I18N = True
USE_L10N = True
USE_TZ = True

But when I add this field:

dateOrder = models.DateTimeField(auto_now_add=True)

into my Order object, when initializing and printing it using

def getDateOrder(self) method:
     weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
     return weekdays[self.dateOrder.weekday()] + ', ' + self.dateOrder.strftime("%d/%m/%Y, %H:%M")

is still UTC format

Looking forward to receiving feedback from everyone

1

There are 1 best solutions below

0
On BEST ANSWER

If you want to get datetime as the timezone that you set in settings.py just call datetime using .astimezone()

dateOrder.astimezone()