How do I get the day of week given a date?

1,066,240

Solution 1

Use weekday():

>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
>>> datetime.datetime.today().weekday()
4

From the documentation:

Return the day of the week as an integer, where Monday is 0 and Sunday is 6.

Solution 2

If you'd like to have the date in English:

from datetime import date
import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()]  #'Wednesday'

Solution 3

If you'd like to have the date in English:

from datetime import datetime
datetime.today().strftime('%A')
'Wednesday'

Read more: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

Solution 4

Use date.weekday() when Monday is 0 and Sunday is 6

or

date.isoweekday() when Monday is 1 and Sunday is 7

Solution 5

I solved this for a CodeChef question.

import datetime
dt = '21/03/2012'
day, month, year = (int(x) for x in dt.split('/'))    
ans = datetime.date(year, month, day)
print (ans.strftime("%A"))
Share:
1,066,240
frazman
Author by

frazman

Updated on January 17, 2022

Comments

  • frazman
    frazman over 2 years

    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 it's Friday)

  • radtek
    radtek almost 10 years
    One important thing to note is that in JavaScript 0 = Sunday, Python starts with 0 = Monday. Something that I ran into, front-end vs back-end..
  • Johnny Utahh
    Johnny Utahh about 9 years
    This seems to be the best answer to generate an English, day-of-week date. I'm guessing it's not upvoted more simply because the answer is ~1 month old, while the question is ~3 years old.
  • ox.
    ox. almost 8 years
    The question asks for Sunday == 1, Monday == 2, and Friday == 6.
  • mrooney
    mrooney over 7 years
    If you'd like Sunday to be day 0: int(datetime.datetime.today().strftime('%w'))
  • Aman Kumar
    Aman Kumar almost 7 years
    To start from 1, we can use isoweekday in place of weekday; 1 = Monday
  • Jens
    Jens almost 7 years
    Because the op asked with regards to a datetime object (not a date object) I’d like to mention that the datetime class sports the same weekday() and isoweekday() methods.
  • himanshu219
    himanshu219 about 6 years
    why do we need to do aux+100 / 400 instead of aux/400 can you please explain
  • Tom Russell
    Tom Russell over 5 years
    'A' for effort! You might move statements, like those assigning to fg and fj, inside the conditional to prevent unnecessary computations.
  • Lekr0
    Lekr0 over 5 years
    There is no need of using DayL array as you can directly get day name by using strftime("%A") instead of weekday()
  • Nathan Tew
    Nathan Tew over 5 years
    I find it much more effective to just do my_date.strftime('%A')
  • Nebulosar
    Nebulosar over 4 years
    Why would you use +1? It is common sence that the weeknumbering in python starts at sundat as 0 and monday as 1.
  • chabir
    chabir about 4 years
    python3: just change all '/' with '//' in the function above and it will work like a charm.
  • Liu Yu
    Liu Yu over 3 years
    In Pandas 1.0 and after, weekday_name() has been changed to day_name()
  • Cornelis
    Cornelis over 3 years
    @NathanTew 1) is "my_date" a variable? 2) can this be used in a query/aggregation? I want to count the average number of meals ordered on mondays/tuesdays/wednesdays. Each order is on a new line, and just like this question, the weekday has to be retrieved from the data-time column of the csv file that we've just uploaded to elasticsearch.
  • Cornelis
    Cornelis over 3 years
    Will this edit the my elasticsearch data/columns? For me this would enable the use of bucket aggregations on weekdays for example.
  • DikShU
    DikShU over 3 years
    but there was Wednesday on 05/08/2015
  • rborodinov
    rborodinov over 3 years
    What about .strftime('%A') link to get a weekday name.
  • not2savvy
    not2savvy over 3 years
    @himanshu219 Because there's an exception from the leap year rule every 400 years, and aux was derived from year -1700, so we need to add 100 to make it a multiple of 400. For example: 2000 - 1700 = 300, so + 100 gives us 400. Not sure though why aux is used to determine the leap year in that lime and not just year itself.
  • Lennart Rolland
    Lennart Rolland almost 3 years
    Who in their right mind would think the week starts at Sunday. Of course it starts at Saturday xD
  • xjlin0
    xjlin0 almost 3 years
    instead of .weekday() try isoweekday()
  • nsky80
    nsky80 over 2 years
    Input given is DD MM YYYY format, perhaps you're interpreting it as MMDDYYYY format.