Kenny_I
Kenny_I

Reputation: 2503

How to set time zone to datetime now in Python?

I would like to call current datetime.now() with GMT +0 (UK) time zone. My workstation is in other timezone.

How to do it?

Upvotes: 2

Views: 8615

Answers (2)

Danil Perestoronin
Danil Perestoronin

Reputation: 1153

Try this:

Before Python 3.6 you can use pytz (external package)

from datetime import datetime
from pytz import timezone    

print(datetime.now(timezone('Europe/London')))

Update according @WolfgangKuehn comment

Starting from Python 3.6 you can use zoneinfo via standard library (>=3.9) or backport instead of pytz

from datetime import datetime
from zoneinfo import ZoneInfo

print(datetime.now(ZoneInfo('Europe/London')))

Upvotes: 6

cyborg
cyborg

Reputation: 594

from datetime import datetime, timezone
print(datetime.now(timezone.utc))

Upvotes: 1

Related Questions