import datetime from dateutil.relativedelta import relativedelta d = datetime.datetime(2015, 1, 1) e = d + relativedelta(years=1) hd = 0 while d < e: if d.month == sum([int(n) for n in list(str(d.day))]): hd += 1 d += relativedelta(days=1) print(hd)