2

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

num3ri
  • 822
  • 16
  • 20

4 Answers4

0

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
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Still getting `NameError: name 'date' is not defined` with this. – Salvatore Mar 29 '19 at 19:02
  • Still getting this _TypeError: unsupported operand type(s) for %: 'datetime.timedelta' and 'int'_ – Abinash Tech Apr 03 '19 at 16:21
  • You've likely overridden `date` as a global variable elsewhere in your module. Make sure to rename all your variables named `date` in the module to something else. – blhsing Apr 03 '19 at 16:23
0

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.

Chris
  • 4,009
  • 3
  • 21
  • 52
0

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?

Salvatore
  • 10,815
  • 4
  • 31
  • 69
0

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.

tnaekc
  • 1
  • 1