how to get weekday from a given date in python?

Question:

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

Asked By: Ali Hamza

||

Answers:

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

Python doc

Answered By: Alix Eisenhardt

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()
Answered By: Amit Wolfenfeld

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
Answered By: Philip Evans
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.