计算时间差
Posted libragyf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算时间差相关的知识,希望对你有一定的参考价值。
# 计算时间差的功能 # 如果是年月日,只显示年月 # 2018-9-3 22:48:20 # 2019-9-4 22:48:20 import time str1 = ‘2020-2-3 22:48:0‘ str2 = ‘2019-9-4 22:48:20‘ struct_time1 = time.strptime(str1, "%Y-%m-%d %H:%M:%S") stamp1 = time.mktime(struct_time1) struct_time2 = time.strptime(str2, "%Y-%m-%d %H:%M:%S") stamp2 = time.mktime(struct_time2) t = stamp1-stamp2 ret = time.gmtime(t) print(‘{}年 {}月 {}日 {}小时 {}分钟 {}秒‘ .format (ret.tm_year-1970, ret.tm_mon-1, ret.tm_mday-1, ret.tm_hour, ret.tm_min, ret.tm_sec))
import time str1 = ‘2020-2-3 22:48:0‘ str2 = ‘2019-9-4 22:48:20‘ def time_diff(time_start, time_end, fmt="%Y-%m-%d %H:%M:%S"): def stamp_func(t): return time.mktime(time.strptime(t, fmt)) stamp1 = stamp_func(time_end) stamp2 = stamp_func(time_start) t = abs(stamp1-stamp2) ret = time.gmtime(t) return (ret.tm_year-1970, ret.tm_mon-1, ret.tm_mday-1, ret.tm_hour, ret.tm_min, ret.tm_sec) ret = time_diff(str2, str1) print(ret)
以上是关于计算时间差的主要内容,如果未能解决你的问题,请参考以下文章
Vue3官网-高级指南(十七)响应式计算`computed`和侦听`watchEffect`(onTrackonTriggeronInvalidate副作用的刷新时机`watch` pre)(代码片段