0

I have a date data that saved in my postgresql it looks like this date

I'm getting the date using this django models:

ddate = list(MyModel.objects.values('created_at'))
getDates = [d['created_at'] for d in ddate if 'created_at' in d]
print(getDates) 

the print output will look like this print output which is, if I compared the time zone from postgres and time zone from my python they are not accurate. How can I fix this?

my model.py looks like this:

class MyModel(models.Model):
    created_at = models.DateTimeField(auto_now=True)
    class Meta:
        db_table = "tablename"

I tried using the this but, I'm having an error because my data is a list of dates it's says that "'list' object has no attribute 'replace'"

utctime = getDates //list of dates// 
from_zone = tz.gettz("UTC") 
to_zone = tz.gettz("mytimezone") 
utctime = utctime.replace(tzinfo=from_zone) 
new_time = utctime.astimezone(to_zone) 
print(utctime)
  • 1
    in PostgreSQL DateTime will be saved in UTC format. this might be helpful https://stackoverflow.com/a/73231751/16250404 – Hemal Patel Sep 26 '22 at 08:50
  • Try reading the documentation, [this](https://docs.djangoproject.com/en/4.1/topics/i18n/timezones/) may help you – Shayan Sep 26 '22 at 10:11
  • Does this answer your question? [How to set the timezone in Django](https://stackoverflow.com/questions/29311354/how-to-set-the-timezone-in-django) – Shayan Sep 26 '22 at 10:13
  • i tried using all of your suggestion but my data is a list of date so I'm having an error it says "'list' object has no attribute 'replace'" – Cason Mercadejas Sep 26 '22 at 11:02
  • utctime = getDates //list of dates// from_zone = tz.gettz("UTC") to_zone = tz.gettz("mytimezone") utctime = utctime.replace(tzinfo=from_zone) new_time = utctime.astimezone(to_zone) print(utctime) – Cason Mercadejas Sep 26 '22 at 11:03
  • 1
    @CasonMercadejas you cant use that function for list of dates. its for Single datetime obj. if you want it to use it for list and then you have to iterate it using For loops and make that logic in common function and pass your datetime obj as a argument. – Hemal Patel Sep 26 '22 at 11:48

0 Answers0