當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python datetime.timetz()用法及代碼示例


timetz() 函數操作 DateTime 模塊的 DateTime 類的對象。該函數使用通過引用賦予它的實例方法,並對其進行轉換,並返回具有相同小時、分鍾、秒、微秒、折疊和 tzinfo 屬性的時間對象。

用法:timetz()

參數:此函數不接受任何參數。

Return values:此函數返回具有相同指定小時、分鍾、秒、微秒、折疊和 tzinfo 的時間對象。

範例1:顯示 timetz() 函數應用的 Python 程序。



Python3


# Python3 code for getting
# a time object with the same
# specified hour, minute, second,
# microsecond, fold and tzinfo.
  
# Importing datetime module
import datetime
  
# Creating a datetime instance
A = datetime.datetime(2021, 8, 3, 10, 11, 12, 13)
  
# Calling the timetz() function over
# the above datetime instance
B = A.timetz()
  
# Printing the original date time object
print("Original date time object:", A)
  
# Printing the new time object
print("New time object:", B)
輸出
Original date time object:2021-08-03 10:11:12.000013
New time object:10:11:12.000013

範例2:展示 timetz() 函數應用的 Python 程序。

Python3


# Python3 code for getting
# a time object with the same
# specified hour, minute, second,
# microsecond, fold and tzinfo.
  
# Importing datetime module
import datetime
  
# Creating a current datetime instance
A = datetime.datetime.now()
  
# Calling the timetz() function over
# the above current datetime instance
B = A.timetz()
  
# Printing the original current date time object
print("Original current date time object:", A)
  
# Printing the new current time object
print("New current time object:", B)
輸出
Original current date time object:2021-08-05 07:41:24.147260
New current time object:07:41:24.147260

範例3:顯示 timetz() 函數應用的 Python 程序。

Python3


# Python3 code for getting
# a time object with the same
# specified hour, minute, second,
# microsecond, fold and tzinfo.
  
# Importing datetime module
import datetime
  
# Creating datetime instances
A = datetime.timedelta(hours=12, minutes=12)
obj = datetime.timezone(A, name="IST")
B = datetime.datetime(2012, 1, 2, 3, 10, 15, 20, obj)
  
# Calling the timetz() function over the above
# specified datetime
C = B.timetz()
  
# Printing the Original datetime object
print("Original datetime object:", B)
  
# Printing the time object with tzinfo attributes
print("Time object with tzinfo attributes:", C)
  
# Printing the time object without tzinfo attributes
print("Time object without tzinfo attributes:", B.time())
輸出
Original datetime object:2012-01-02 03:10:15.000020+12:12
Time object with tzinfo attributes:03:10:15.000020+12:12
Time object without tzinfo attributes:03:10:15.000020




相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Python datetime.timetz() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。