in the below code, I am trying to compare dates in a column in my excel but getting this error: 2022-03-18 10:18:16' does not match format '%Y-%d-%m %H:%M:%S'
for i in sheet2['G']:
if i.value=='Transaction Time':
sheet2['G1']='Transaction Time'
elif i.value=='None':
sheet2.delete_rows(i.row)
elif datetime.datetime.strptime(str(i.value),'%Y-%m-%d %H:%M:%S')<date1:
sheet2.delete_rows(i.row)
elif datetime.datetime.strptime(str(i.value),"%Y-%d-%m %H:%M:%S")>date2:
sheet2.delete_rows(i.row)
CodePudding user response:
Just a typo i guess
elif datetime.datetime.strptime(str(i.value),'%Y-%m-%d %H:%M:%S')<date1:
sheet2.delete_rows(i.row)
elif datetime.datetime.strptime(str(i.value),"%Y-%d-%m %H:%M:%S")>date2:
sheet2.delete_rows(i.row)
Your first one is good %Y-%m-%d %H:%M:%S
but the second one, you inverted day and month format "%Y-%d-%m %H:%M:%S"
CodePudding user response:
Finally, I got it with just few lines of codes.
I removed the time on the date to make it easy for comparison with these codes:
for i in sheet2['G']: for r in range (2,mr2 1): i.value= str(i.value)[0:10] sheet2['G1']='Transaction Date'
With this success, I was able to filter any date I want using the following:
for i in sheet2['G']: if str(i.value)!=date1: sheet2.delete_rows(i.row)
where date1 is a variable that I can modify or the app user can modify to get any day of interest.