Skip to content Skip to sidebar Skip to footer

Python - Datetime.now() Returns Incorrect Time

I want to store in my data base the time stamp of an operation. The problem is that the time that I get from datetime.datetime.now()is incorrect (I guess it's due to time zone). I

Solution 1:

Try this adjusting the number of hours depending on the timezone you are:

import datetime 

datetime.datetime.utcnow()+datetime.timedelta(hours=3)

Solution 2:

This works for me:

import pytz
importdatetimetimezone= pytz.timezone('Europe/Madrid')
now = datetime.datetime.now(tz = timezone)

install pytz: pip install pytz

get all timezones: print(pytz.all_timezones)

Post a Comment for "Python - Datetime.now() Returns Incorrect Time"