Python基础-计算时间差,时间和,精确到秒,微秒,毫秒

时间差

import datetime
starttime = datetime.datetime.now()
#long running
endtime = datetime.datetime.now()
print (endtime - starttime).seconds
print (endtime - starttime).microseconds

1
235478

时间和

starttime = datetime.datetime.now()
endtime = starttime + datetime.timedelta(hours=10)
print(starttime,endtime)

2021-03-17 17:38:51.727371 2021-03-18 03:38:51.727371

timedelta中支持的参数:

days: float = ...,
seconds: float = ...,
microseconds: float = ..., #微秒
milliseconds: float = ..., #毫秒
minutes: float = ...,
hours: float = ...,
weeks: float = ...,

获取秒级时间戳与毫秒级时间戳、微秒级时间戳

import time

t = time.time()

print (t)                       #原始时间数据
print (int(t))                  #秒级时间戳
print (int(round(t * 1000)))    #毫秒级时间戳
print (int(round(t * 1000000))) #微秒级时间戳

1615974455.0226276
1615974455
1615974455023
1615974455022628

你可能感兴趣的:(Python,python,机器学习,深度学习,时间,秒)