美文网首页
python3获取当天剩余时间

python3获取当天剩余时间

作者: qinbatista | 来源:发表于2019-08-13 17:48 被阅读0次

我不知道这么简单的一个问题,全网搞这么复杂,不是时间搓转时间,就是time类和datetime类相互使用。。几行代码就ok了,复制粘贴即可运行,最后结果给出的是秒,需要转换自己可以转换

from datetime import datetime, timedelta

    current_time1 = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    print(current_time1)#打印当天时间

    current_time2 = (datetime.now()+timedelta(days=1)).strftime("%Y-%m-%d 00:00:00")

    print(current_time2)#打印当天凌晨时间,其实就是加一天,是分秒全部0

    d1 = datetime.strptime(current_time1, '%Y-%m-%d %H:%M:%S')

    d2 = datetime.strptime(current_time2, '%Y-%m-%d %H:%M:%S')

    print(((d2-d1).total_seconds())/60/60)#打印剩余时间

相关文章

网友评论

      本文标题:python3获取当天剩余时间

      本文链接:https://www.haomeiwen.com/subject/qrwbjctx.html