import datetime import time import calendar def cal(year, month): first_day = 1 # monday = firstday first_of_month = datetime.date(year, month, 1) day_names = [] n = 0 t = (3 + first_day) * 86400 while n < 7: day_names.append(time.strftime('%A', time.gmtime(t))) n += 1 t += 86400 arr = first_of_month.strftime('%m,%Y,%B,%w').split(',') weekday = (int(arr[3]) + 7 - first_day) % 7 if calendar.isleap(year): feb = 29 else: feb = 28 days_in_month = [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] day = 1 dim = days_in_month[month - 1] d = [] tmp = [] if weekday > 0: for i in range(weekday): tmp.append(0) while day <= dim: if weekday == 7: weekday = 0 d.append(tmp) tmp = [] tmp.append(day) day += 1 weekday += 1 d.append(tmp) print d