在Python中處理Unix時間戳可以使用datetime
模塊。以下是一些處理Unix時間戳的技巧:
import datetime
timestamp = 1626182400 # Unix時間戳
dt_object = datetime.datetime.fromtimestamp(timestamp)
print(dt_object)
import datetime
dt_object = datetime.datetime(2021, 7, 13, 0, 0) # 日期時間對象
timestamp = int(dt_object.timestamp())
print(timestamp)
import time
timestamp = int(time.time())
print(timestamp)
import datetime
timestamp = 1626182400 # Unix時間戳
dt_object = datetime.datetime.fromtimestamp(timestamp)
formatted_date = dt_object.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_date)
這些技巧可以幫助你在Python中更好地處理Unix時間戳。