63

Please suggest me on the following. How to find whether a particular day is weekday or weekend in Python?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
vinay h
  • 777
  • 1
  • 5
  • 7

2 Answers2

180

You can use the .weekday() method of a datetime.date object

import datetime

weekno = datetime.datetime.today().weekday()

if weekno < 5:
    print "Weekday"
else:  # 5 Sat, 6 Sun
    print "Weekend"
ti7
  • 16,375
  • 6
  • 40
  • 68
fahad
  • 2,999
  • 3
  • 17
  • 20
29

Use the date.weekday() method. Digits 0-6 represent the consecutive days of the week, starting from Monday.

Jacob
  • 123
  • 8
Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55