首页 > 解决方案 > 在python中以日期时间格式获取当前小时和分钟

问题描述

我想在 python 中以特定格式获取当前时间:

  1. 'H:M' 格式
  2. 'datetime.time' 类

这是因为我试图以这种确切的格式将当前时间与另一个时间进行比较,我从一个 excel 文件中获取。

当我尝试明显的:

>>> from datetime import datetime
>>> now = datetime.now()
>>> current_time = (now.hour, now.minute)
>>> current_time > other_time

(“其他时间”例如“datetime.time(8, 30)”)

我得到:

TypeError: '>' not supported between instances of 'datetime.time' and 'tuple'

我尝试了各种各样的东西并搜索了所有东西,但找不到任何适合我的东西,它总是归结为不受支持的实例,如 'tuple' 'str' 或其他。

非常感谢您的帮助。

标签: pythonclasstimecompare

解决方案


推荐阅读