Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I want to find out the following: given a date (datetime object), what is the corresponding day of the week.

For instance, Sunday is the first day, Monday: second day.. and so on

And then if the input is something like today's date.

Example

>>> today = datetime.datetime(2017, 10, 20)

>>> today.get_weekday() # what I look for

The output is maybe 6 (since its Friday)

1 Answer

0 votes
by (106k points)

For getting the day of the week in Python we use weekday()function. This function returns weak day as an integer, where Monday is represented as 0 and Sunday as 6:-

>>> import datetime

>>> datetime.datetime.today()

>>> datetime.datetime.today().weekday()

image

You are seeing the output as 5 because I am writing this answer on Saturday.

Related questions

0 votes
1 answer
asked Jul 9, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Jul 26, 2019 in Python by selena (1.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...