【问题标题】:python compare two date strings with different formatpython比较两个不同格式的日期字符串
【发布时间】:2020-08-16 01:59:44
【问题描述】:

开始时间的格式为日期时间:

YYYY-MM-DDTHH:MM:SS.SSSZ (e.g., 2004-08-04T19:09:02.768Z)

我的代码

starttime = item['ListingDetails']['StartTime']
present_date = datetime.now() - timedelta(days=2)
startdate = datetime.strptime(starttime, 'WhichFormat')
if  startdate.date() < present_date.date():
    print('Relisting item :', str(itemid), starttime)

抛出异常是因为我不知道哪个是真正的格式。

目标是查看开始时间是否是 2 天前。 I.E 如果物品在 2 天前重新上架 + 那么现在就重新上架

【问题讨论】:

标签: python


【解决方案1】:

如果我理解正确,你可以使用 dateutil.parser:

import dateutil.parser

starttime = item['ListingDetails']['StartTime']
present_date = datetime.now() - timedelta(days=2)
startdate = dateutil.parser.parse('2004-08-04T19:09:02.768Z')
if  startdate.date() < present_date.date():
    print('Relisting item :', str(itemid), starttime)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多