Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
To convert between local time and UTC time in Python, you can use the pytz
library, which provides comprehensive timezone support. If you don't have the library installed, you can install it using pip:
pip install pytz
Here's an example of converting a local time to UTC time and vice versa using the pytz
library:
from datetime import datetime import pytz # Create a datetime object representing the local time local_dt = datetime.now() # Set the timezone for the local time local_tz = pytz.timezone("America/New_York") # Replace with your local timezone localized_dt = local_tz.localize(local_dt) # Convert the local time to UTC time utc_dt = localized_dt.astimezone(pytz.utc) print(f"Local time: {localized_dt}") print(f"UTC time: {utc_dt}") # Convert the UTC time back to local time converted_local_dt = utc_dt.astimezone(local_tz) print(f"Converted local time: {converted_local_dt}")
In this example, we first create a datetime
object representing the current local time. Then, we set the timezone for the local time using the timezone()
function from the pytz
library. After that, we use the localize()
method to attach the timezone information to the datetime
object.
To convert the local time to UTC time, we use the astimezone()
method with pytz.utc
as the argument. This will return a new datetime
object with the time converted to UTC.
Finally, to convert the UTC time back to local time, we use the astimezone()
method again with the local timezone as the argument.
Note: Make sure to replace "America/New_York"
with the appropriate timezone for your local time.
Python convert local time to UTC:
from datetime import datetime, timezone local_time = datetime.now() utc_time = local_time.astimezone(timezone.utc) print(utc_time)
Convert UTC to local time in Python:
from datetime import datetime, timezone utc_time = datetime.utcnow() local_time = utc_time.astimezone(timezone.utc).astimezone() print(local_time)
Python pytz library for timezone conversion:
from datetime import datetime import pytz local_time = datetime.now(pytz.timezone('America/New_York')) utc_time = local_time.astimezone(pytz.utc) print(utc_time)
Convert string to datetime with timezone in Python:
from datetime import datetime import pytz date_string = "2023-01-01T12:00:00" datetime_with_tz = datetime.fromisoformat(date_string).replace(tzinfo=pytz.timezone('America/New_York')) print(datetime_with_tz)
Convert timestamp to UTC time in Python:
from datetime import datetime, timezone timestamp = 1609459200 # Example timestamp utc_time = datetime.utcfromtimestamp(timestamp).replace(tzinfo=timezone.utc) print(utc_time)
Convert ISO 8601 string to UTC time in Python:
from datetime import datetime, timezone iso_string = "2023-01-01T12:00:00Z" utc_time = datetime.fromisoformat(iso_string).replace(tzinfo=timezone.utc) print(utc_time)
Convert datetime to timestamp in UTC in Python:
from datetime import datetime, timezone utc_time = datetime.now(timezone.utc) timestamp = int(utc_time.timestamp()) print(timestamp)
Using dateutil.tz for timezone conversion in Python:
from datetime import datetime from dateutil import tz local_time = datetime.now(tz.gettz('America/New_York')) utc_time = local_time.astimezone(tz.tzutc()) print(utc_time)
Python arrow library for datetime with timezone:
import arrow local_time = arrow.now('America/New_York') utc_time = local_time.to('UTC') print(utc_time)
Convert between UTC and local time with pytz in Python:
from datetime import datetime import pytz local_time = datetime.now(pytz.timezone('America/New_York')) utc_time = local_time.astimezone(pytz.utc) print(utc_time) # Convert back to local time converted_local_time = utc_time.astimezone(pytz.timezone('America/New_York')) print(converted_local_time)
Python strftime for timezone-aware datetime:
from datetime import datetime import pytz local_time = datetime.now(pytz.timezone('America/New_York')) formatted_string = local_time.strftime("%Y-%m-%d %H:%M:%S %Z") print(formatted_string)
UTC to local time conversion with timedelta in Python:
from datetime import datetime, timedelta, timezone utc_time = datetime.utcnow().replace(tzinfo=timezone.utc) local_time = utc_time - timedelta(hours=5) # Adjust based on the timezone offset print(local_time)
Convert datetime to UTC timestamp in Python:
from datetime import datetime, timezone local_time = datetime.now() utc_timestamp = int(local_time.replace(tzinfo=timezone.utc).timestamp()) print(utc_timestamp)