While creating a Python program, we may have to get the current time following a timezone. Below lines of code works for the purpose.
1 2 3 4 5 6 |
from datetime import datetime from pytz import timezone tz = timezone('the time zone') # exmaple: tz = timezone('EST') datetime.now(tz) ## this returns a datetime object pointing to right now ## according to the timezone info object handed in as the tz variable. |
In order to show all timezones, let following below command
1 2 3 4 5 6 7 |
import pytz pytz.all_timezones Output: ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', ...] |
I am a Freelancer in programming specifically Python Scripting, Web scraping, and Web automation with 10+ years of experience.