programming-examples/python/Date/Program to select all the Sundays of a specified year.py

13 lines
385 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
from datetime import date, timedelta
def all_sundays(year):
# January 1st of the given year
dt = date(year, 1, 1)
# First Sunday of the given year
dt += timedelta(days = 6 - dt.weekday())
while dt.year == year:
yield dt
dt += timedelta(days = 7)
for s in all_sundays(2020):
print(s)