0
published_date = 7/11/2019 at 06:04 AM,7/11/2019 at 1:04 AM,7/11/2019 at 5:36 AM,7/11/2019 at 04:00 PM

All times are in EST. I need to fetch data from time range of 00.00 - 07.00 AM(EST).It should skip the time above 7.00AM(EST)

if published_date.time() <= 7:00AM print(published_date.time())

Actual results:

published_date = 7/11/2019 at 06:04 AM,7/11/2019 at 1:04 AM,7/11/2019 at 5:36 AM,7/11/2019 at 04:00 PM

Expected results:

published_date = 7/11/2019 at 06:04 AM,7/11/2019 at 1:04 AM,7/11/2019 at 5:36 AM
abi
  • 7
  • 5

1 Answers1

0

Your date are not in correct format. Hence it will not parse and compare it correct. You need to use datetime library to convert the strings into date and compare. Here is the working example:

from datetime import datetime
import datetime as dt

datetime_object = datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')
checkTime = dt.time(7,0,0)
published_dates = ["7/11/2019 06:04 AM","7/11/2019 1:04 AM","7/11/2019 5:36 AM","7/11/2019 04:00 PM"]
filtered_dates = []
for d in published_dates:
  dt1 = datetime.strptime(d, '%d/%m/%Y %I:%M %p')
  if (dt1.time() < checkTime):
      filtered_dates.append(dt1.strftime("%d/%m/%Y %I:%M %p"))
print('filtered_dates : ', filtered_dates)
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • fr time range between 7AM-12PM what will be the condition it shuld inculde 7AM datas too – abi Jul 22 '19 at 12:11
  • If you want to fetch datetime between a timerange, use condition like `7 < time< 12`. P.S. datetime hours are in 24 hour format. – Nishu Tayal Jul 22 '19 at 12:13
  • How can I chnge in the above logic what will be the checktime – abi Jul 22 '19 at 12:15
  • Add if confition as `if( checkTime < dt1.time()) and (dt1.time() < dt.time(12,0,0)): range_dates.append(dt1.strftime("%d/%m/%Y %I:%M %p"))` Here is the example : https://repl.it/repls/ModernPriceyScreenscraper – Nishu Tayal Jul 22 '19 at 12:22
  • for the timerange between 12pm to 11:59pm if( checkTime < dt1.time()) and (dt1.time() < dt.time(11,59,0)) and checkTime = dt.time(12,0,0) is this crct – abi Jul 23 '19 at 05:05