Python - datetime.now() returns incorrect time

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
import datetime

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

install pytz: pip install pytz

get all timezones: print(pytz.all_timezones)