在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时间戳。