6

2001-10-18 I want to calculate weekday e.g. Monday, Tuesday from the date given above. is it possible in python?

Ali Hamza
  • 121
  • 1
  • 1
  • 5

3 Answers3

11

Here is one way to do this:

dt = '2001-10-18'
year, month, day = (int(x) for x in dt.split('-'))    
answer = datetime.date(year, month, day).weekday()
Amit Wolfenfeld
  • 603
  • 7
  • 9
6

There is the weekday() and isoweekday() methods for datetime objects.

Python doc

ChaimG
  • 7,024
  • 4
  • 38
  • 46
Alix Eisenhardt
  • 313
  • 2
  • 10
0

Here's what I have that got me if a leap year and also days in a given month (accounts for leap years). Finding out a specific day of the week, that I'm also stuck on.

def is_year_leap(year):
    if (year & 4) == 0:
        return True
    if (year % 100) == 0:
        return False
    if (year % 400) == 0:
        return True
    return False

def days_in_month(year, month):
    if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
        return 31
    if month == 2:
        if is_year_leap(year):
            return 29
        else:
            return 28
    if month == 4 or month == 6 or month == 9 or month == 11:
        return 31
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103