0

I have made this code that should return whether a specific date is a weekend or a weekday but always returns weekday. What's wrong? I am new to python so any help would be appreciated :)

import datetime
def sunday_saturday_definer():
date_for_checking = datetime.datetime(day = 10, month= 12, year= 2019)
if date_for_checking.weekday == 5:
    print("weekend")
elif date_for_checking.weekday == 6:
    print("weekend")
else:
    print("Weekday")
sunday_saturday_definer()
Spider Wings
  • 71
  • 1
  • 6
  • 1
    Possible duplicate: https://stackoverflow.com/questions/29384696/how-to-find-current-day-is-weekday-or-weekends-in-python – PySaad Dec 01 '19 at 09:24
  • 1. It's always the same date. 2. Weekday is a *method*. – jonrsharpe Dec 01 '19 at 09:26
  • Did you research here for `date`+ `weekday`+`python`? This question answers it: https://stackoverflow.com/questions/9847213/how-do-i-get-the-day-of-week-given-a-date – hc_dev Dec 01 '19 at 09:37
  • What did you expect? The date `2019-12-10` is a _Tuesday_, hence `weekday` is expected in this case – hc_dev Dec 01 '19 at 09:55

2 Answers2

1

use date_for_checking.weekday()

Shahad Ishraq
  • 341
  • 2
  • 11
1

Your code evaluating date_for_checking.weekday returns the reference to the function, because you missed the parentheses.

If you want to call the function use parentheses:

is_weekend = date_for_checking.weekday() in [5,6]
is_weekday = date_for_checking.weekday() in range(0,5)

See the docs for date.weekday():

Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a Wednesday. See also isoweekday().

hc_dev
  • 8,389
  • 1
  • 26
  • 38