Q:

Check if date is in string

I have a dictionary of values returned from a form:
planning_settings = {'days_clear': u'3', 'day_planned': u'0', 'dates_blocked': u'03/15/2014,03/16/2014,03/17/2014', 'star_method': u'1'}

I am iterating over a set number of days:
for i in range( int(planning_settings['day_planned']), int(planning_settings['day_planned']) + 15 ):
    d = date.today() + timedelta(days=i)

I want to check whether d is a date that is within planning_settings['dates_blocked'].   How should I slice the string u'03/15/2014,03/16/2014,03/17/2014' and compare it to the datetime.date object, date.today() + timedelta(days=i)? 

A:

One approach is to convert your datetime.date object into a string and then check to see if that string is in your list of dates (which is split on the comma):
>>> from datetime import date, timedelta
>>> dates_blocked = u'03/15/2014,03/16/2014,03/17/2014'
>>> some_date = date.today() + timedelta(days=1)  # tomorrow
>>> some_date = date.strftime(some_date, '%m/%d/%Y')  # formats some_date into this format: 03/15/2014
>>> if some_date in dates_blocked.split(','):
    print some_date

03/16/2014

So your code might look like this:
>>> planning_settings = {'days_clear': u'3', 'day_planned': u'0', 'dates_blocked': u'03/15/2014,03/16/2014,03/17/2014', 'star_method': u'1'}
>>> first_day = int(planning_settings['day_planned'])
>>> for i in range(first_day, first_day + 15):  # next two weeks
    d = date.today() + timedelta(days=i)
    d = date.strftime(d, '%m/%d/%Y')
    if d not in planning_settings['dates_blocked'].split(','):  # assumes you want available dates
        print d, 'available'

03/18/2014 available
03/19/2014 available
03/20/2014 available
03/21/2014 available
03/22/2014 available
03/23/2014 available
03/24/2014 available
03/25/2014 available
03/26/2014 available
03/27/2014 available
03/28/2014 available
03/29/2014 available

