Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I have a timezone aware timestamptz field in PostgreSQL. When I pull data from the table, I then want to subtract the time right now so I can get its age.

The problem I'm having is that both datetime.datetime.now() and datetime.datetime.utcnow()seem to return timezone unaware timestamps, which results in me getting this error:

TypeError: can't subtract offset-naive and offset-aware datetimes

Is there a way to avoid this (preferably without a third-party module being used).

1 Answer

0 votes
by (106k points)

If you can't subtract offset-naive and offset-aware datetimes you should try to remove the timezone awareness use the following:-

naive = dt.replace(tzinfo=None)

You may have to add time zone conversion as well. Another point to note that be aware of the age of this answer.

Related questions

0 votes
1 answer
asked Jul 31, 2019 in Python by Eresh Kumar (45.3k points)
0 votes
1 answer
asked Jul 30, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
+1 vote
1 answer
asked Aug 3, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...