slpcf slpcf - 1 month ago 8
Python Question

How to find the number of days between two dates in format %Y%m%d if one of the day is the list element?

today = datetime.datetime.now().strftime ("%Y%m%d")


I have today date. Now I want to see the difference between a date as a list element in the same format and today's date. How can I?

Edit : This is my code -

today = datetime.datetime.now().strftime ("%Y%m%d")

for i,val in enumerate(date):
d = date[i] = datetime.datetime.strptime (val,"%d-%b-%Y").strftime ("%Y%m%d")
# our dates are now converted into 20160101 format
delta = date(today) - date(d)
if delta.days < 3:
print "true"
else:
print "false"

Answer

Not sure why you are converting back to a string when you have the date objects already:

import datetime

today = datetime.datetime.now()
dates = ['10-Jan-2016', '24-Oct-2016']

for val in dates:
    date = datetime.datetime.strptime(val, '%d-%b-%Y')
    delta = today - date
    if delta.days < 3:
        print("true")
    else:
        print("false")

Output:

false
true
Comments