gpt4 book ai didi

python - 尝试在Python中减去两个日期并在几小时内(大于24小时时间段)得到结果

转载 作者:太空宇宙 更新时间:2023-11-03 16:39:25 25 4
gpt4 key购买 nike

尝试在 python 中减去两个日期并在几小时内(大于 24 小时时间段)得到结果

这是我尝试过的:

    currentdate = datetime.now()
# gets the date from the website
lastcrawl = driver.find_element_by_xpath('//*[@id="last-15336"]/h4').text
# convert string into time format
formatlastcrawl = datetime.strptime(lastcrawl, '%b %d %H:%M')
# converts into the time format - April 28, 11:24
newformatlastcrawl = formatlastcrawl.strftime('%b %d %H:%M')
print newformatlastcrawl
# substracts 24 hours of the current date(because the website crawl happens every 24 hours) + 2 hours(to give enough time to finish the crawl)
substractdate = (currentdate - timedelta(hours=26)).strftime("%b %d %H:%M")
print substractdate
# changes the format of the current date. For example - Nov 12
formatcurrentdate = currentdate.strftime("%b %d %H:%M")
print formatcurrentdate

a = (substractdate - newformatlastcrawl)
print a

当我尝试减去它时,我收到此错误:

a = (substractdate - newformatlastcrawl).strptime("%b %d %H:%M")类型错误:不支持的操作数类型 -:“str”和“str”

我想在几小时内得到答案。此外,时间值可以大于 24 小时

最佳答案

该程序以小时为单位打印两个时间之间的差异。

from datetime import datetime

now = datetime.now()

# Problem: no year specified!
b = datetime.strptime('Apr 24 11:24', '%b %d %H:%M')

# Fix: Use the current year
b = b.replace(year=now.year)

# Goal: print number of hours between the two dates
diff_in_hours = (now - b).total_seconds() / 3600
print '{} - {} = {} hours'.format(now, b, diff_in_hours)

关于python - 尝试在Python中减去两个日期并在几小时内(大于24小时时间段)得到结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36947557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com