在 Python 中表示没有日期的时间的结构 [英] Structure representing time without date in Python

查看:38
本文介绍了在 Python 中表示没有日期的时间的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 数据结构中是否存在仅表示小时分和秒而没有其他信息的情况,如果没有,那么在我的情况下最好使用哪种结构?

解决方案

为什么不直接使用 datetime 模块及其相关功能:

from datetime import datetimemy_time = '05:43:34'my_time = datetime.strptime('%H:%M:%S', my_time)

然后您可以简单地执行以下操作:

<预><代码>>>>my_time.hour5>>>我的时间.分钟43>>>my_time.second34

Is there in Python data structure to represent only hour minute and second without another info, if not then what structure is best to use in my case?

解决方案

Why not just use the datetime module and its relevant features for that:

from datetime import datetime
my_time = '05:43:34'

my_time = datetime.strptime('%H:%M:%S', my_time)

Then you can simply do things like:

>>> my_time.hour
5

>>> my_time.minute
43

>>> my_time.second
34

这篇关于在 Python 中表示没有日期的时间的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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