Python get days in a month output 0

Question:

When ever I ran this the output is always 0 where i was expecting like 29 or 30.

import datetime

def days_in_month(year, month):
    """
    Inputs:
      year  - an integer between datetime.MINYEAR and datetime.MAXYEAR
              representing the year
      month - an integer between 1 and 12 representing the month

    Returns:
      The number of days in the input month.
    """

    #if month is december, we proceed to next year
    def month_december(month):
        if month == 12:
            return 1
        else:
            return month

    #if month is december, we proceed to next year
    def year_december(year, month):
        if month == 12:
            return new_year + 1
        else:
            return year

    #verify if month/year is valid
    if (month < 1) or (month > 12):
        print ("please enter a valid month")
    elif (year < 1) or (year > 9999):
        print ("please enter a valid year between 1 - 9999")
    else:
        #subtract current month from next month then get days
        date1 = (datetime.date(year_december(year, month), month_december(month), 1) - datetime.date(year, month, 1)).days
        print (date1)

days_in_month(1997, 1)
Asked By: Christian

||

Answers:

You forgot to add one month at first parameter.
Your codes:

datetime.date(year_december(year, month), month_december(month), 1)
= datetime.date(year, month, 1)

New codes:

date1 = (datetime.date(year_december(year, month+1), month_december(month+1), 1) - datetime.date(year, month, 1)).days

But pay an attention on the result will be wrong if December, you need to improve it like below:

Full codes:

import datetime

def days_in_month(year, month):
    """
    Inputs:
      year  - an integer between datetime.MINYEAR and datetime.MAXYEAR
              representing the year
      month - an integer between 1 and 12 representing the month

    Returns:
      The number of days in the input month.
    """

    #if month is december, we proceed to next year
    def month_december(month):
        if month > 12:
            return month-12  #minus 12 if cross year.
        else:
            return month

    #if month is december, we proceed to next year
    def year_december(year, month):
        if month > 12:
            return year + 1
        else:
            return year

    #verify if month/year is valid
    if (month < 1) or (month > 12):
        print ("please enter a valid month")
    elif (year < 1) or (year > 9999):
        print ("please enter a valid year between 1 - 9999")
    else:
        #subtract current month from next month then get days
        date1 = (datetime.date(year_december(year, month+1), month_december(month+1), 1) - datetime.date(year, month, 1)).days
        print (date1)

days_in_month(1997, 12)
days_in_month(1998, 1)
days_in_month(1998, 2)

Test Case

days_in_month(1997, 12)
days_in_month(1998, 1)
days_in_month(1998, 2)

Output

31
31
28
[Finished in 0.187s]
Answered By: Sphinx

As daniel told there is a standard library method. It is always better to reuse than reinvent.

import calendar

def days_in_month(year, month):
    if (month < 1) or (month > 12):
        print ("please enter a valid month")
    elif (year < 1) or (year > 9999):
        print ("please enter a valid year between 1 - 9999")
    else:
        return calendar.monthrange(year, month)[1]
Answered By: Arun Karunagath

There is a really cool method I like, and you don’t have to import anything. You just need to have two one-line functions. For input you need the year (as integer) and month (as integer) – which both comes from the date it self.
So:

  1. function: check if it’s a leap year or not.

  2. function: create a list of the days (corrected with leap year) and call the item of it with the month.

    def is_leap_year(year):
    return ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)

    def get_days_in_month(year, month):
    return [31, (29 if is_leap_year(year) else 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month – 1]

Check it out.

Answered By: sipi09
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.