【问题标题】:python change the date format using strftimepython使用strftime更改日期格式
【发布时间】:2017-02-20 07:16:52
【问题描述】:

在 python 中,我如何更改日期的格式在两个日期之间减去:

lease_expiry_date = '{}'.format(t.lease_expiry_date.strftime("%d %B %Y"))
lease_commencement_date = '{}'.format(t.lease_commencement_date.strftime("%d %B %Y"))
years = t.lease_expiry_date - t.lease_commencement_date
p.add_run('{}'.format(years))

结果是:

358 days, 0:00:00

我只想使用 strftime 显示 358 天

【问题讨论】:

  • 能否给出输入的日期时间格式以便找到解决办法。

标签: python date-format strftime


【解决方案1】:

您的对象 years 在 Python 中是一个 timedelta 对象,因此您可以指定感兴趣的字段,例如:

'{}'.format(years.days())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2020-11-17
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    相关资源
    最近更新 更多