簡體   English   中英

python 比較兩個不同格式的日期字符串

[英]python compare two date strings with different format

開始時間采用日期時間格式:

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 天前。 IE 如果項目在 2 天前重新上架 + 然后現在重新上架

如果我理解正確,您可以使用 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM