Python 计算时间差

  1. 字符串格式转换为时间格式
    timekeeper[0],timekeeper[-1]为字符串格式的日期形式,如“2019-11-15 17:32:24”
    start = time.strptime(timekeeper[0], “%Y-%m-%d %H:%M:%S”)
    end = time.strptime(timekeeper[-1], “%Y-%m-%d %H:%M:%S”)

  2. 计算时间差
    时间格式直接做差,会报错,需将其转为日期形式,如果没有年份,则默认1900年
    start = datetime.datetime(start[0], start[1], start[2], start[3], start[4], start[5])
    end = datetime.datetime(end[0], end[1], end[2], end[3], end[4], end[5])
    date2-date1#返回时差 hh:mm:ss
    (date2-date1).days #返回日期差
    (date2-date1).seconds #返回秒差

发布了16 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_38308549/article/details/103089284