【问题标题】:How to remove seconds from Python datetime object? [duplicate]如何从 Python 日期时间对象中删除秒数? [复制]
【发布时间】:2019-05-25 04:46:16
【问题描述】:

我有一个想要在网站上显示的 python 日期时间对象,但是时间以 hh:mm:ss 格式显示,我想以 hh:mm 格式显示它。

我已尝试使用以下替换方法:

message.timestamp.replace(second='0', microsecond=0)

但是这并没有变红,它只是用 hh:mm:00 替换它

【问题讨论】:

    标签: python


    【解决方案1】:

    使用strftime()函数

    >>> from datetime import datetime
    >>> datetime.now()
    datetime.datetime(2018, 12, 27, 11, 14, 37, 137010)
    >>> now = datetime.now()
    >>> now.strftime("%H:%M")
    '11:15'
    

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 2023-03-30
      相关资源
      最近更新 更多