2001-10-18 I want to calculate weekday e.g. Monday, Tuesday from the date given above. is it possible in python?
Asked
Active
Viewed 2.6k times
6
-
yes it's possible, did you try something ? – Dadep May 19 '17 at 12:20
-
did you try searching for already existing questions? – Norbert Forgacs May 19 '17 at 12:20
-
http://stackoverflow.com/questions/9847213/which-day-of-week-given-a-date-python check this one out – Norbert Forgacs May 19 '17 at 12:20
-
3Possible duplicate of [which day of week given a date python](http://stackoverflow.com/questions/9847213/which-day-of-week-given-a-date-python) – gonczor May 19 '17 at 12:20
3 Answers
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.

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

Philip Evans
- 1
- 1