python中如何获取当前时间,并计算时间差

import datetime
import time
#法一:推荐方法 因为时间获取后可读性高 且可计算时间差
curr_time = datetime.datetime.now()
print(curr_time) #输出:2020-08-04 18:53:33.463004
print(type(curr_time)) #输出:<class 'datetime.datetime'>
time.sleep(3)
curr_time2 = datetime.datetime.now()
print(curr_time2-curr_time)#输出: 0:00:03.000171
#法二:计算的时间差和获取的当前时间打出来是浮点数 可读性不高 
time1 = time.time()
print(time1) 输出:1596538120.9242718
print(type(time1)) 输出:<class 'float'>
time.sleep(3)
time2 = time.time()
print(time2-time1) 输出:3.000171661376953
posted @ 2020-08-05 10:12  菜鸟到大佬  阅读(13091)  评论(0编辑  收藏  举报