0

I am trying to create an array that will house the days of each month from the CURRENT MONTH until a future date that is specified (for this case I will use 12/31/2022).

So far I am able to set it up to get each day of each month in a given year, but only starting in January.

What I need to have happen is, for example starting now in September, have the array created with days in a month for SEPTEMBER, OCTOBER, NOVEMBER...DECEMBER of 2022.

The code I have thus far:

total = 0
days = []
dayst = []
for i in range(1,13):
    month = datetime.datetime.strptime('{}'.format(i), "%m").strftime("%B")
    length_of_month = calendar.monthrange(MODELYEAR, i)[1]
    total = total + length_of_month
    days.append(length_of_month)
    dayst.append(total)
    totaldays = sum(days)

With current output:

[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

I essentially just need to make the end time dynamic and shift the start time to current month.

Thank you all!

martineau
  • 119,623
  • 25
  • 170
  • 301
bathtub2007
  • 81
  • 1
  • 7
  • Possible duplicate of [How to iterate over a timespan after days, hours, weeks and months in Python?](https://stackoverflow.com/questions/153584/how-to-iterate-over-a-timespan-after-days-hours-weeks-and-months-in-python) – Vishnudev Krishnadas Sep 16 '19 at 16:36

3 Answers3

1

Use the datetime library to get the number of days between two dates

>>> import calendar
>>> from dateutil import rrule
>>> from datetime import datetime
>>> end = datetime.strptime('12/31/2022', '%m/%d/%Y')
>>> start = datetime.now()
>>> [calendar.monthrange(dt_i.year, dt_i.month)[1] for dt_i in rrule.rrule(rrule.MONTHLY, dtstart=start, until=end)]
[30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
0

What you need is to get the first day of each month of interest, and compute the number of days to the first of the next month after it.

import datetime

def days(ending):
    now = datetime.datetime.now()
    next_month = datetime.datetime(now.year, now.month, 1)
    while next_month <= ending:
        month = next_month
        y = month.year
        m = month.month + 1
        if m > 12:
            y += 1
            m = 1
        next_month = datetime.datetime(y, m, 1)
        yield (next_month - month).days
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

Basically you need to determine the days in each month from the current date to a future date. Here's what I think is a very straight-forward way of doing exactly that:

import calendar
import datetime


def days_per_month_until_end_date(end_year, end_month, end_day):
    today = datetime.datetime.today()
    future_date = datetime.datetime(end_year, end_month, end_day)
    result = []
    for year in range(today.year, future_date.year+1):
        start_month = today.month if year == today.year else 1
        end_month = future_date.month if year == future_date.year else 12
        for month in range(start_month, end_month+1):
            dim = calendar.monthlen(year, month)  # Days In Month
            result.append(dim)
    return result


print(days_per_month_until_end_date(2022, 12, 31))

Result printed from running it on 2019-09-16 with the end date as shown):

[30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
martineau
  • 119,623
  • 25
  • 170
  • 301