简体   繁体   中英

Finding Days Per Month From Now Until End Date

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!

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]

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM