Particles

datetime模块

#datetime()模块

from datetime import datetime
print(datetime.now())   # 获取到当前的时间,对象
>>>2019-09-26 18:12:06.156978
print(datetime(2016,11,13,12,13,14)) #指定时间
>>>2016-11-13 12:13:14

#时间戳与对象转换

from datetime import datetime
import time
print(datetime.fromtimestamp(time.time()))  #时间戳转换成对象
>>>2019-09-26 18:10:53.236499    
print(datetime.timestamp(datetime.now()))   #将当前时间对象转成时间戳
>>>1569492653.236498

#对象和字符串转换

from datetime import datetime
print(datetime.strftime(datetime.now(),"%Y-%m-%d %H:%M:%S")) #将当前时间对象转成字符串
print(type(datetime.strftime(datetime.now(),"%Y-%m-%d %H:%M:%S")))
>>>2019-09-26 18:14:45
>>><class 'str'>
print(datetime.strptime("2019/10/14","%Y/%m/%d"))  #将字符串时间转成当前时间对象
print(type(datetime.strptime("2019/10/14","%Y/%m/%d")))
>>>2019-10-14 00:00:00
>>><class 'datetime.datetime'>

时间相减

print(datetime.now() - datetime(9999,11,1,12,13,14))
>>>-2914671 days, 6:02:30.885245

from datetime import datetime,timedelta
print(datetime.now())
>>>2019-09-26 18:17:38.402255
print(datetime.now() - timedelta(days=400))
>>>2018-08-22 18:17:38.402255
print(datetime.now() - timedelta(days=500))
>>>2018-05-14 18:17:38.402255

posted @ 2020-01-08 14:37  Na_years  阅读(100)  评论(0编辑  收藏  举报