python计算时间_python的时间使⽤和时间计算
python存在两个时间类time/dateTime
区别:
time:在python⽂档中,time是归类在常规操作系统服务中,它提供的功能更加接近于操作系统层⾯。其所能表述的⽇期范围被限定在1970-2038之间,如果需要表述范围之外的⽇期,可能需要考虑使⽤datetime模块更好。
datetime:⽐time⾼级了不少,可以理解为datetime基于time进⾏了封装,提供了更多实⽤的函
⽤法:
简介时间元组(struct_time):
tm_year:年1-12
tm_mon:⽉1-12
tm_mday:⽇1-31
tm_hour:时0-23
tm_min:分0-59
tm_sec:秒0-59
tm_wday:星期0-6(0表⽰周⽇)
tm_day:⼀年中的第⼏天1-366
tm_isdst:是否是夏令,默认为-1
time:
time.sleep(sec):推迟指定时间sec后继续运⾏
time.localtime([sec]):将⼀个时间戳转化成⼀个当时时区的struct_time,如果sec参数未输⼊,则以当前时间为转化标准
time.strftime(format[,t]):将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
time.time():返回当前时间的时间戳(以秒表⽰的浮点数)
time.mktime(t):将⼀个struct_time转换为时间戳
time.clock():不同系统含义不同。UNIX——返回进程时间,WINS第⼀次调⽤返回进程运⾏的实际时间,第⼆次调⽤是⾃第⼀次调⽤以后到现在的运⾏时间
time.asctime([t])把⼀个时间的元组或者struct_time表⽰为“Sun Jun 20 23:21:05 1993”,如果⽆参数,则会把time.localtime()作为参数传⼊
time.strftime(format[,t]):把⼀个代表时间的元组或者struct_time转化为格式化的时间字符串。如果t为指定,将传⼊time.localtime()。如果元组中任何⼀个元素越界,ValueError的错误将会被抛出
datetime:
提供类⽅法如下:
timedelta:主要⽤于计算时间跨度
tzinfo:时区相关
time:只关注时间
date:只关注⽇期
datetime:同时有时间和⽇期
实际使⽤中⽤的⽐较多的是:datetime.datetime 和 datetime.timedelta
datetime.day
datetime.hour
datetime.minute
datetime.second
datetime.microsecond
datetime.date():返回date对象python格式化输出format
datetime.time():返回time对象
datetime.timetuple():返回time.struct_time 对象
datetime.strftime(format):按照format进⾏格式化输出
计算时间精确到时分秒:
from datetime importdatetime
oldtime= datetime(2020,10,26, 11,00,38)
newtime= datetime(2020,10,26, 18,00,38)print ((newtime-oldtime).seconds) #秒print (round((newtime-oldtime).seconds/60)) #分
print (round((newtime-oldtime).seconds/3600)) #时
print ((newtime-oldtime).days) #⽇
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论