Python时间转换h:m:s到秒 [英] Python Time conversion h:m:s to seconds

查看:454
本文介绍了Python时间转换h:m:s到秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,使用timedelta功能,您可以使用以下方式将秒数转换为h:m:s:

I am aware that with the timedelta function you can convert seconds to h:m:s using something like:

>> import datetime
>> str(datetime.timedelta(seconds=666)) 
'0:11:06'

但是我需要将h:m:s转换为秒或分钟。

But I need to convert h:m:s to seconds, or minutes.

你知道一个可以做到这一点的功能吗?

Do you know a function that can do this?

推荐答案

def hms_to_seconds(t):
    h, m, s = [int(i) for i in t.split(':')]
    return 3600*h + 60*m + s

这篇关于Python时间转换h:m:s到秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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