Python Check if Date is Weekend or Weekday Example

By Hardik Savani October 30, 2023 Category : Python

Today our leading topic is python check if date is weekend or weekday. This post will give you simple example of python check if date is weekend. This tutorial will give you simple example of python check if date is weekday. this example will help you python find if date is weekend. So, let's follow few step to create example of python datetime check if date 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.

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

I hope it can help you...

Tags :
Shares