简体   繁体   中英

Check the day of the week in Python

I am trying to write code for checking whether it is Sunday or Saturday. But I am getting following error:

The code is:

import datetime
def isholiday(date):

    dat0 = date(2019, 3, 23)  # Saturday
    dat1 = date(2019, 3, 24)  # Sunday
    if abs(date - dat0) % 7 == 0 | abs(date - dat1)%7 == 0 :
        print ("is holiday")
    return True

In the next line I have:

assert(isholiday(datetime.datetime(2018, 10, 21))==True)

The error is:

     ------------------------------------------------------------------------ 
        ---
        TypeError                                 Traceback (most recent call 
        last)
        <ipython-input-22-87d6e5a340a0> in <module>()
        ----> 1 assert(isholiday(datetime.datetime(2018, 10, 21))==True)

        <ipython-input-21-bb556bebaa01> in isholiday(date)
              2 from datetime import date
              3 def isholiday(date):
        ----> 4     dat0 = date(2019,3,23)
              5     dat1 = date(2019,3,24)
              6     if abs(date - dat0)%7 == 0 | abs(date - dat1)%7 == 0 :

        TypeError: 'datetime.datetime' object is not callable

The parameter date of your isholiday function shadows the datetime.date class. You should rename the parameter to avoid such a naming conflict:

def isholiday(dat):
    dat0 = date(2019,3,23) #Saturday
    dat1 = date(2019,3,24)  #Sunday
    if abs(dat - dat0)%7 == 0 | abs(dat - dat1)%7 == 0 :
        print ("is holiday")
        return True

I think you mean this:

dat0 = datetime.datetime(2019,3,23) #Saturday

dat1 = datetime.datetime(2019,3,24)  #Sunday

Currently, you are using the 'date' argument in the function and then "calling" it with parentheses and arguments. This is only used when initialising it.

Use weekday() . Also, your function should always return something, not only sometimes return True .

from datetime import date

def isholiday(dat):
    if dat.weekday() == 6 or dat.weekday == 5: # 6 is Sunday, 5 is Saturday 
        print ("is holiday")
        return True
    return False

assert(isholiday(date(2018, 10, 21))==True)

Gives:

is holiday

Possible duplicate of: How do I get the day of week given a date in Python?

I modified your code like this:

import datetime
def isholiday(date):

    dat0 = datetime.datetime(2019,3,23) #Saturday
    dat1 = datetime.datetime(2019,3,24)  #Sunday
    if abs((date - dat0).days)%7 == 0 or abs((date - dat1).days)%7 == 0:
        print ("is holiday")
        return True

assert(isholiday(datetime.datetime(2018, 10, 21))==True)

You are trying to call the 'date' argument and you can't call a datetime.dateime object like this. I changed dates in 5th and 6th line with datetime.datetime. Also you can't use abs() function with datetime.timedelta objects like you use with integers so you should get only the 'day'. I added .days for it.

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