Skip to content

Instantly share code, notes, and snippets.

@lancejohnson
Created August 26, 2020 16:40
Show Gist options
  • Save lancejohnson/4b1610029074dc7783b6102b46b58a28 to your computer and use it in GitHub Desktop.
Save lancejohnson/4b1610029074dc7783b6102b46b58a28 to your computer and use it in GitHub Desktop.
import calendar
def get_first_and_last_day_for_a_year(year_num):
first_and_last_dates = []
first_and_last_dates_formatted = []
for i in range(1, 13):
_, num_days = calendar.monthrange(year_num, i)
first_day = datetime.date(year_num, i, 1)
last_day = datetime.date(year_num, i, num_days)
first_and_last_dates.append([first_day, last_day])
formatted_first_and_last_date = [first_day.strftime('%Y-%m-%d')]
formatted_first_and_last_date.append(last_day.strftime('%Y-%m-%d'))
first_and_last_dates_formatted.append(formatted_first_and_last_date)
return first_and_last_dates, first_and_last_dates_formatted
first_and_last_dates = get_first_and_last_day_for_a_year(2016)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment