在python中尝试减去两个日期并以小时(大于24小时的时间段)为单位获得结果
在python中尝试减去两个日期并以小时(大于24小时的时间段)为单位获得结果 这就是我尝试过的:在python中尝试减去两个日期并以小时(大于24小时的时间段)为单位获得结果,python,selenium,time,timedelta,Python,Selenium,Time,Timedelta,在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 formatlast
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).strTime(“%b%d%H:%M”)
TypeError:-:“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)
与标题相关:如果utc偏移量不固定(例如,如果本地时区遵守DST),则使用原始的datetime对象,如
datetime.now()返回的datetime对象可能会导致错误的时差。