
For example, from datetime import datetime For that, we will pass the pytz.UTC as argument to astimezone() function. Using astimezone(), we can convert it to UTC timezone and get the current UTC. Suppose we have a datetime object that contains the current time in the local timezone, and it has the timezone information associated with it. Output: Datetime in Local Time zone: 09:10:34.300030ĭatetime in UTC Time zone: 03:40:34.300030+00:00 Convert the current local time to UTC in Python Latest Python - Video Tutorial from datetime import datetime Instead of string, if you already have the datetime object with local timezone (or null timezone), we can convert it directly to the datetime object with UTC timezone using astimezone(). Output: Datetime in Local Time zone: 08:18:19ĭatetime in UTC Time zone: 02:48:19+00:00ĭatetime string in UTC Time zone: 02:48:19 Convert timezone of datetime object from local to UTC in Python Print('Datetime string in UTC Time zone: ', dt_utc_str) Print('Datetime in UTC Time zone: ', dt_utc) # Convert local datetime to UTC time-zone datetime Print('Datetime in Local Time zone: ', local_dt) Local_dt = datetime.strptime(dt_str, format) # Create datetime object in local timezone Let’s use this to convert local time to UTC i.e. It returns a new DateTime instance according to the specified time zone parameter tz, i.e., it converts the time in calling datetime to the specified timezone and returns a new datetime object containing it. The astimezone() function accepts a timezone instance tz as an argument. Python: Get difference between two datetimes in minutes.Python: Get difference between two dates in years.
#Python get utc time how to
Python : How to convert a timestamp string to a datetime object using datetime.strptime().Python - Access Nth item in List Of Tuples Python - Check if a value is in Dictionary # UTC - 3:00 (Argentina, Brazil, Chile, Paraguay, Uruguay, and Falkland Islands time zone)ĭt3 = datetime.Python - Returning Multiple Values in Function # UTC + 7:00 (Vietnam, Thailand, and Indonesia time zone)ĭt2 = datetime.now(tz=timezone(timedelta(hours=7))) # UTC + 5:00 (Pakistan, India, Bangladesh, Nepal, Sri Lanka, and Bhutan time zone)ĭt1 = datetime.now(tz=timezone(timedelta(hours=5))) In my opinion, this method has a small advantage in that you will have a harder time getting typos than the 2 approaches above.Įxample: from datetime import datetime, timedelta, timezone You can also set a timezone by UTC offset. Indonesia_time = omtimestamp(time.time(), tz=pytz.timezone('Asia/Jakarta'))Īustralia_time = datetime.now(tz=pytz.timezone('Australia/Sydney')) Japan_time = datetime.now(tz=pytz.timezone('Asia/Tokyo')) Pakistan_time = datetime.now(pytz.timezone('Asia/Karachi'))Ĭhina_time = datetime.now(pytz.timezone('Asia/Shanghai')) Germany_time = datetime.now(pytz.timezone('Europe/Berlin'))Įgypt_time = omtimestamp(time.time(), tz=pytz.timezone('Africa/Cairo'))
#Python get utc time install
This module needs to be installed first: pip install pytz If you are working with an old version of Python (prior to 3.9), you can set the time zone by using the pytz module.

Specifying time zone with the pytz module


If you want to print out the results in a nicer format, see this article: How to Format Date and Time in Python. Note that the results you receive depend on when you run the code. Vietnam_dt = datetime.now(tz=ZoneInfo("Asia/Ho_Chi_Minh")) India_dt = datetime.now(tz=ZoneInfo("Asia/Kolkata")) Korea_dt = datetime.now(tz=ZoneInfo("Asia/Seoul")) Turkey_dt = datetime.now(tz=ZoneInfo("Europe/Istanbul")) Us_western_dt = omtimestamp(time.time(), tz=ZoneInfo("America/Los_Angeles")) Us_eastern_dt = datetime.now(tz=ZoneInfo("America/New_York"))
#Python get utc time download
Get all links from a webpage with Beautiful SoupĮxtract and download all images from a webpage Get the Current Date and Time with Timezone The modern Python regular expressions cheat sheetĬapitalize the first letter of each word in a stringĬompare 2 strings ignoring case sensitivityĬount the frequency of each word in a stringĬonvert Datetime to Timestamp and vice versa Generating a Random Float between Min and Max Generate a Random Integer between Min and Max
