在python中将时间(HH:MM:SS)转换为分钟 [英] Convert time (HH:MM:SS) to minutes in python

查看:57
本文介绍了在python中将时间(HH:MM:SS)转换为分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将HH:MM:SS转换为分钟?

How to convert HH:MM:SS to Minutes?

我在下面尝试了以下方法:

I tried below works fine:

  import datetime as DT
  t1 = DT.datetime.strptime('0:10:00', '%H:%M:%S')

我尝试了另一个示例并出现错误:

I tried another example and getting error:

  import datetime as DT
  t1 = DT.datetime.strptime('9715:56:46', '%H:%M:%S')

错误:

  ValueError: time data '9715:56:46' does not match format '%H:%M:%S'

在此示例中,它支持%H的Max 23,如果我的小时数超过23(在这种情况下,我有9715小时),如何获得它?

In this example it support Max 23 for %H, If my hours has more than 23(in this case i have 9715 hours) how to get it?

推荐答案

您可以将 sum 与生成器表达式或 map 一起使用:

You can use sum with a generator expression or map:

from operator import mul

my_time = '9715:56:46'
factors = (60, 1, 1/60)

t1 = sum(i*j for i, j in zip(map(int, my_time.split(':')), factors))
t2 = sum(map(mul, map(int, my_time.split(':')), factors))

print(t1)  # 582956.7666666667

assert t1 == t2

这篇关于在python中将时间(HH:MM:SS)转换为分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆