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"))
Author by
frazman
Updated on January 17, 2022Comments
-
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 almost 10 yearsOne 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 about 9 yearsThis 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. almost 8 yearsThe question asks for Sunday == 1, Monday == 2, and Friday == 6.
-
mrooney over 7 yearsIf you'd like Sunday to be day 0:
int(datetime.datetime.today().strftime('%w'))
-
Aman Kumar almost 7 yearsTo start from 1, we can use isoweekday in place of weekday; 1 = Monday
-
Jens almost 7 yearsBecause the op asked with regards to a
datetime
object (not adate
object) I’d like to mention that thedatetime
class sports the sameweekday()
andisoweekday()
methods. -
himanshu219 about 6 yearswhy do we need to do aux+100 / 400 instead of aux/400 can you please explain
-
Tom Russell over 5 years'A' for effort! You might move statements, like those assigning to
fg
andfj
, inside the conditional to prevent unnecessary computations. -
Lekr0 over 5 yearsThere is no need of using DayL array as you can directly get day name by using
strftime("%A")
instead ofweekday()
-
Nathan Tew over 5 yearsI find it much more effective to just do
my_date.strftime('%A')
-
Nebulosar over 4 yearsWhy would you use
+1
? It is common sence that the weeknumbering in python starts at sundat as 0 and monday as 1. -
chabir about 4 yearspython3: just change all '/' with '//' in the function above and it will work like a charm.
-
Liu Yu over 3 yearsIn Pandas 1.0 and after, weekday_name() has been changed to day_name()
-
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 over 3 yearsWill this edit the my elasticsearch data/columns? For me this would enable the use of bucket aggregations on weekdays for example.
-
DikShU over 3 yearsbut there was Wednesday on 05/08/2015
-
rborodinov over 3 yearsWhat about
.strftime('%A')
link to get a weekday name. -
not2savvy over 3 years@himanshu219 Because there's an exception from the leap year rule every 400 years, and
aux
was derived fromyear -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 whyaux
is used to determine the leap year in that lime and not justyear
itself. -
Lennart Rolland almost 3 yearsWho in their right mind would think the week starts at Sunday. Of course it starts at Saturday xD
-
xjlin0 almost 3 yearsinstead of .weekday() try isoweekday()
-
nsky80 over 2 yearsInput given is DD MM YYYY format, perhaps you're interpreting it as MMDDYYYY format.