Recently I had a requirement to get all the dates of Saturdays and Sundays in a particular year. I wrote a more generic function to implement the same. This function accepts start date and end dates and finds all the dates for a given list of week days in day_list parameter. Week day number starts with Monday being 0 (zero) to 6 (Sunday). So, If you have to find all the Mondays in the year, pass  in the day_list parameter. from_date and end_date are python date objects.
def get_dates(from_date, to_date, day_list=[5,6]):
tmp_list = list()
date_list = list()
## Creates a list of all the dates falling between the from_date and to_date range
for x in xrange((to_date - from_date).days+1):
tmp_list.append(from_date + datetime.timedelta(days=x))
for date_record in tmp_list:
if date_record.weekday() in day_list:
Here day_list is the week number list, for which dates are to be found and from_date and to_date make a date range where these dates have to be scanned in.
Latest posts by Ankit Jaiswal (see all)
- Creating HTML5 forms elements in Django forms - June 9, 2015
- Generating raw SQL query for Django model definition to create table schema manually - December 5, 2014
- Django – Incorrect integer value: ‘True’ for column ‘is_superuser’ at row 1 - November 14, 2014