Python datetime.utcoffset() Method
-
Syntax of Python
datetime.utcoffset()
Method -
Example Codes: Use the
datetime.utcoffset()
Method in Python -
Example Codes: Use the
datetime.utcoffset()
Method to Find the Offset of Time
Python datetime.utcoffset()
method is an efficient way of finding the difference between the wall clock
time in the current zone and the wall clock
time in UTC.
Syntax of Python datetime.utcoffset()
Method
datetime.utcoffset()
Parameters
This datetime.utcoffset()
method does not accept any parameters.
Return
The return type of this method is a time delta object representing the difference between the UTC and the local time.
Note that if the offset
lies on the east of UTC, it takes on a positive value, and if the offset
is west of UTC, it obtains a negative value. Since the total hours in a day is 24, -timedelta(24)
and timedelta(24)
are the largest values.
Example Codes: Use the datetime.utcoffset()
Method in Python
from datetime import datetime
import pytz
datetime_object = datetime.now()
print(datetime_object.utcoffset())
Output:
None
The above code produces "None"
because the now()
function returns the date and time in UTC format. So, the difference between the local time and UTC is 0, meaning "None"
.
Example Codes: Use the datetime.utcoffset()
Method to Find the Offset of Time
from datetime import datetime
import pytz
datetime_object = datetime.now()
tz = pytz.timezone("Asia/Tokyo")
difference = tz.localize(datetime_object)
print("The time difference between UTC and local time is ", difference.utcoffset())
Output:
The time difference between UTC and local time is 9:00:00
The method datetime.utcoffset
is used in the DateTime
class of module DateTime
.
Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.
LinkedIn