convert eastern time to pacific time in python

To convert Eastern Time to Pacific Time in Python, you can use the datetime module and its timezone class.

Here's an example code snippet that demonstrates how to convert an Eastern Time object to a Pacific Time object:

main.py
from datetime import datetime
from pytz import timezone

# create a datetime object for the current time in Eastern Time
eastern_time = datetime.now(timezone('US/Eastern'))

# convert the Eastern Time object to Pacific Time
pacific_time = eastern_time.astimezone(timezone('US/Pacific'))

# print the original and converted times
print('Eastern Time:', eastern_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print('Pacific Time:', pacific_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
473 chars
13 lines

In this code snippet, we first create a datetime object for the current time in Eastern Time using the now() method and passing in a timezone object for the Eastern Timezone.

We then call the astimezone() method on the Eastern Time object, passing in a timezone object for the Pacific Timezone, which converts the Eastern Time object to a Pacific Time object.

Finally, we print out the original and converted times using the strftime() method to format the datetime objects as strings.

gistlibby LogSnag