Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import calendar
- from dateutil.relativedelta import relativedelta
- def nth_date(month, nth):
- try:
- month = month.replace(month.year, month.month, nth)
- except ValueError:
- month = month.replace(month.year, month.month, calendar.monthrange(month.year, month.month)[1])
- return month
- def dates(start, end, nth):
- """
- Given a start and end date, return a list of dates at nth date each month
- If nth is greater than last day of month, set to last day of month instead.
- """
- start = nth_date(start, nth)
- months = [start]
- rd = relativedelta(end, start)
- total_months = (12 * rd.years) + rd.months
- for _ in range(total_months):
- start += relativedelta(months=1)
- start = nth_date(start, nth)
- months.append(start)
- return months
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement