Python3——时间&日期库使用

import time   #主要用于时间的查看
#输出当前时间戳
print(time.time())
#获取当前的日期
print(time.strftime('%Y-%m-%d'))
# 时间戳转日期
print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

print('=====================')

import datetime  #大多用于时间的计算
print(datetime.datetime.now())

# 计算十分钟之后的时间
# 构造函数:datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
# 其中参数都是可选,默认值为0
newtime = datetime.timedelta(minutes=10)
print(datetime.datetime.now()+newtime) # 当前时间加上10分钟

# 计算10天以后的时间
one_day = datetime.datetime(2019,10,13)
new_date = datetime.timedelta(days=10)
print(one_day+new_date) # 2019-10-23 00:00:00

# 2019-10-14 11:26:07.313722     ".313722" 是指0.313722秒  秒在计算机里面不是最小的单位

猜你喜欢

转载自www.cnblogs.com/gjh99/p/12103551.html