Computer Science, asked by srivarsha4449, 1 year ago

How do I print a Python datetime in the local timezone?

Answers

Answered by acronsharp8
0

Answer:

import datetime

import pytz

timestring = "2017-05-30T23:51:03Z"

# Create datetime object

d = datetime.datetime.strptime(timestring, "%Y-%m-%dT%H:%M:%SZ")

print(d.tzinfo) # Return time zone info

print(d.strftime("%d.%m.%y %H:%M:%S"))

# Set the time zone to 'Europe/Paris'

d = pytz.timezone('Europe/Paris').localize(d)

print(d.tzinfo) # Return time zone info

# Transform the time to UTC

d = d.astimezone(pytz.utc)

print(d.tzinfo) # Return time zone info

print(d.strftime("%d.%m.%y %H:%M:%S"))

Similar questions