python时间操作
2022-07-05 15:07 Bughra 阅读(23) 评论(0) 编辑 收藏 举报>>> import time
>>> import datetime
>>> time_now = int(time.time())
>>> time_now
1551178679
>>> time_local = time.localtime(time_now)
>>> time_local
time.struct_time(tm_year=2019, tm_mon=2, tm_mday=26, tm_hour=18, tm_min=57, tm_sec=59, tm_wday=1, tm_yday=57, tm_isdst=0)
>>> dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
>>> dt
'2019-02-26 18:57:59'
>>> dt=datetime.datetime.now()
>>> dt
datetime.datetime(2019, 2, 26, 18, 59, 9, 819038)
>>> st = "2017-11-23 16:10:10"
>>> st
'2017-11-23 16:10:10'
>>> dt = datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S")
>>> dt
datetime.datetime(2017, 11, 23, 16, 10, 10)
>>>