Printing the Number of Days in a Given Month and Year [Python]

Printing the number of days in a given month and year [Python]

Some syntax error in your code:

  1. There should be no space before def days_in_month(month,year). Python use indentation to separate code blocks. This is the error you given in comment.
  2. There is no elseif in python, it should be elif
  3. output.is_leap_year = True, it should be is_leap_year(year) == True. The False part should be changed too.
  4. after if statement and else there should be a :, like

    if month == 'September' or month == 'April' or month == 'June' or month == 'November':
    print 30
    elif month == 'January' or month == 'March' or month == 'May' or month== 'July' or month == 'August' or month == 'October' or month== 'December':
    print 31
    elif month == 'February' and is_leap_year(year) == True:
    print 29
    elif month == 'February' and is_leap_year(year) == False:
    print 28
    else:
    print 'Blank'

Count days from beginning of year/month to today

The month is really simple, just call .dt.day.

For the year case, you subtract the date from Jan 1 of the same year, and count the number of days.

Assuming opendate is already of type Timestamp:

df['Days since BOM'] = df['opendate'].dt.day
df['Days since BOY'] = (df['opendate'] - (df['opendate'] - pd.tseries.offsets.YearBegin())).dt.days

Thanks to @ChrisA, there's an even simpler solution for the year case:

df['Days since BOY'] = df['opendate'].dt.dayofyear 

How to find number of days in the current month

As Peter mentioned, calendar.monthrange(year, month) returns weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month.

>>> import calendar
>>> print calendar.monthrange(2012,1)[1]
31
>>> calendar.monthrange(2012,2)[1]
29

Edit: updated answer to return the number of days of the current month

>>> import calendar
>>> import datetime
>>> now = datetime.datetime.now()
>>> print calendar.monthrange(now.year, now.month)[1]
29

How to calculate the number of days left in a given year without using the datetime module?

Here's a minimally modified version of your code that does what you've asked:

year = int(input("Enter the year to determine the number of days: "))
#month = int(input("Enter the month of the year: "))
#day = int(input("Enter the day of the year: "))

def if_leap_year(year):

if (year % 400 == 0): return 366

elif (year % 100 == 0): return 365

elif (year % 4 == 0): return 366

else:
return 365

#print(if_leap_year(year))

def days_in_month(month, year):
if month in {1, 3, 5, 7, 8, 10, 12}:
return 31
if month == 2:
if if_leap_year(year):
return 29
return 28
return 30

#print(days_in_month(month, year))

def days_left_in_year(month, day, year):
daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
daysLeft = (if_leap_year(year) if month < 3 else 365) - sum(daysInMonth[:month - 1]) - day
return daysLeft

print(days_left_in_year(1,1,2020))
print(days_left_in_year(3,1,2020))
print(days_left_in_year(1,1,2022))
print(days_left_in_year(3,1,2022))

Output:

365
305
364
305

UPDATE:
I think you're just looking to do this for the second part of your question:

def refund_period():
year = int(input("Enter the year to determine the number of days: "))
month = int(input("Enter the month of the year: "))
day = int(input("Enter the day of the year: "))
if if_leap_year(year) == 365:
money_owed = (days_left_in_year(month, day, year) / 365) * 278
if if_leap_year(year) == 366:
money_owed = (days_left_in_year(month, day, year) / 366) * 278
return money_owed

print(refund_period())

Your function refund_period() was not returning anything, so I added return money_owed, then I added the line print(refund_period()).

Here's a sample run:

Enter the year to determine the number of days: 2020
Enter the month of the year: 02
Enter the day of the year: 29
232.42622950819674

How to get the first and last date of the month with Year and Month in python

From monthrange() you will get the first day and the number of days in that month. You can use the datetime method to convert it to date.

month =3
year= 2022
first, last = calendar.monthrange(year, month)
print(first, last)

print(datetime.datetime(year, month, 1))
print(datetime.datetime(year, month, last))

How to count days belonging to a given month in the range of two Python datetimes?

Iterating over the days would be the most straightforward way to do it. Otherwise, you would need to know how many days are in a given month and you would need different code for different scenarios:

  1. The given month is the month of the first date
  2. The given month is the month of the second date
  3. The given month is between the first and the second date (if dates span more than two months)

If you want to support dates spanning more than one year then you would need the input to include month and year.

Your example fits scenario #1, which I guess you could do like this:

>>> from datetime import datetime, timedelta
>>>
>>> first_date = datetime(2017, 10, 29)
>>>
>>> first_day_of_next_month = first_date.replace(month=first_date.month + 1, day=1)
>>> last_day_of_this_month = first_day_of_next_month - timedelta(1)
>>> number_of_days_in_this_month = last_day_of_this_month.day
>>> number_of_days_in_this_month - first_date.day + 1
3

This is why I would suggest implementing it the way you originally intended and only turning to this if there's a performance concern.



Related Topics



Leave a reply



Submit