Python获取Unix时间戳

来自多彩世界 2020-06-11

时间戳和日期相互转换。

获取当前时间戳并去掉小数

from datetime import datetime

timestamp = datetime.timestamp(datetime.now())
print("timestamp =", int(timestamp))

获取指定日期的时间戳

from datetime import datetime

sometime = datetime(2020, 6, 3, 12, 50)
print(int(datetime.timestamp(sometime)))

把时间戳转为日期

from datetime import datetime

now_date = datetime.fromtimestamp(datetime.timestamp(datetime.now()))
print(now_date)

参考链接: