slpcf slpcf - 1 year ago 92
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 = ("%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 = ("%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"
print "false"

Answer Source

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

import datetime

today =
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:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download