python将多个时间段相加时分秒满60自动向前+1,求最后时间总和
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python将多个时间段相加时分秒满60自动向前+1,求最后时间总和相关的知识,希望对你有一定的参考价值。
A = ['12:06:30', '13:01:00', '11:58:32', '12:08:46', '15:13:17', '12:22:40', '11:49:31', '12:59:53', '12:08:40',
'11:36:18', '11:29:59', '11:13:27', '13:55:53', '13:05:10', '11:09:10', '10:41:00', '11:16:28', '12:33:28']
A = ['12:06:30', '13:01:00', '11:58:32', '12:08:46', '15:13:17', '12:22:40', '11:49:31', '12:59:53', '12:08:40',
'11:36:18', '11:29:59', '11:13:27', '13:55:53', '13:05:10', '11:09:10', '10:41:00', '11:16:28', '12:33:28']
total_time = 0
# 计算列表内所有时间的总秒数
for one in A:
res_time = one.split(':')
hour = int(res_time[0])*3600
minate = int(res_time[1])*60
total_time+=hour+minate+int(res_time[2])
# print(total_time)
# 取模,通过divmod函数获得商和余数
total_hour = divmod(total_time,3600)[0] # 计算小时数
total_min = divmod(divmod(total_time,3600)[1],60)[0] # 计算分钟数
total_s = total_time%3600%60 # 计算秒数
# 格式化输出
print(f'最后时间总和total_time秒,转化为时间是total_hour:total_min:total_s')
题主你好,
代码见截图:
执行结果见截图:
-----
希望可以帮到题主, 欢迎追问.
A = ['12:06:30', '13:01:00', '11:58:32', '12:08:46', '15:13:17', '12:22:40', '11:49:31', '12:59:53', '12:08:40',
'11:36:18', '11:29:59', '11:13:27', '13:55:53', '13:05:10', '11:09:10', '10:41:00', '11:16:28', '12:33:28']
s=[0,0,0]
for e in A:
e=tuple(map(int,e.split(':')))
for i in range(len(e)):
s[i]+=e[i]
for i in range(-1,-3,-1):
s[i]%=60
s[i-1]+=s[i]//60
print(':'.join(map(str,s)))追答
截图:
时间相减得到时分秒
public function secToTime($times){
$result = ‘00:00:00‘;
if ($times>0) {
$hour = floor($times/3600);
$minute = floor(($times-3600 * $hour)/60);
$second = floor((($times-3600 * $hour) - 60 * $minute) % 60);
$result = $this->len($hour).‘:‘.$this->len($minute).‘:‘.$this->len($second);
}
return $result;
}
public function len($str){
if(strlen($str) == 1){
$time = ‘0‘.$str;
}else{
$time = $str;
}
return $time;
}
以上是关于python将多个时间段相加时分秒满60自动向前+1,求最后时间总和的主要内容,如果未能解决你的问题,请参考以下文章