如果要在Python
中获取时间戳,可以使用time
、datetime
模块。
- 获取时间戳
>>> import time
>>> ts1 = time.time()
>>> ts1
1557905910.63995
- 反转
>>> readable = time.ctime(ts1)
>>> readable
'Wed May 15 15:38:30 2019'
- 获取时间戳
>>> import datetime
>>> ts2 = datetime.datetime.now().timestamp()
>>> ts2
1557906112.26428
- 反转
>>> readable = datetime.datetime.fromtimestamp(ts2)
>>> readable
datetime.datetime(2019, 5, 15, 15, 41, 52, 264280)
>>> readable.strftime('%Y-%m-%d %H:%M:%S')
'2019-05-15 15:41:52'