python get day if its in a dates range -
i'm trying check if first date of month , last date of month lies in range of dates (the range 7 days window starting current date) . below example i'm trying achieve:
import datetime, calendar today = datetime.date.today() date_list = [today + datetime.timedelta(days=x) x in range(0, 7)] lastdayofmonth = today.replace(day=calendar.monthrange(today.year,today.month)[-1]) if 1 in [ date_list[i].day in range(0, len(date_list))]: print "we have first day of month in range" elif lastdayofmonth in [ date_list[i].day in range(0, len(date_list))]: print " have last date of month in range"
i'm wondering if there cleaner way doing that? want print exact date if find in list don't know how without expanding loop in if statement , save print date_list[i]
if matches condition. instead of printing message when find first day in range should print actual date. same last date.
thanks in advance!
the thing can come with, without having make use of iteration is:
import datetime, calendar today = datetime.date.today() week_from_today = today + datetime.timedelta(days=6) last_day_of_month = today.replace(day=calendar.monthrange(today.year,today.month)[-1]) if today.month != week_from_today.month: print datetime.date(week_from_today.year, week_from_today.month, 1) elif today <= last_day_of_month <= week_from_today: print last_day_of_month
since today it's 2016-06-02
it's hard test code.
try changing variable today
day. used dates 2016-05-25
, 2016-05-26
test code.
to set custom date: today = datetime.date(yyyy, m, d)