Skip to content

Latest commit

 

History

History
43 lines (32 loc) · 670 Bytes

how-to-get-current-timestamp-and-conversion-in-python.md

File metadata and controls

43 lines (32 loc) · 670 Bytes

Python获取当前时间戳以及转换

如果要在Python中获取时间戳,可以使用timedatetime模块。

time

  • 获取时间戳
>>> import time
>>> ts1 = time.time()
>>> ts1
1557905910.63995
  • 反转
>>> readable = time.ctime(ts1)
>>> readable
'Wed May 15 15:38:30 2019'

datetime

  • 获取时间戳
>>> 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'