How to Check if Date is Weekend or Weekday in Python?

Published On: 22/09/2022 | Category: Python


Hi Guys,

Now, let's see example of python check if date is weekend. We will use python check if date is weekday. you can see python check if date is weekend or weekday. Here you will learn python datetime check if date is weekend. follow bellow step for python check today is weekend.

In this example, we will use datetime library to check date is weekend or weekday. i will give you two simple examples one with current date and another with string date. so let's see both examples:

You can use these examples with python3 (Python 3) version.

So let's see bellow example:

Example 1: Python Check Today's Date is Weekend or Weekday

main.py
from datetime import datetime
  
# Get Day Number from weekday
weekno = datetime.today().weekday()
 
if weekno < 5:
    print("Today is a Weekday")
else:  
    # 5 Sat, 6 Sun
    print("Today is a Weekend")
Output:
Today is a Weekday

Example 2: Python Check String Date is Weekend or Weekday

main.py
from datetime import datetime
  
myDateString = "2022-06-04"
  
myDate = datetime.strptime(myDateString, "%Y-%m-%d")
  
# Get Day Number from weekday
weekno = myDate.weekday()
  
if weekno < 5:
    print("Today is a Weekday")
else:  
    # 5 Sat, 6 Sun
    print("Today is a Weekend")
Output:
Today is a Weekend

It will help you....

Happy Pythonic Coding!