How to get the day of the week from the date in format 20170703

I tried this:

print('1data =', datetime.datetime.today().isoweekday('20170703')) print('2data =', time.strptime(20170703)) print('3data =', datetime.datetime.weekday(20170703)) 
  • Something weird for you, here's how to print(datetime.datetime.strptime('20170703', '%Y%m%d').isoweekday()) - andreymal
  • Thanks, @andreymal! It worked. In the help did not understand the syntax without an example. Your comment helped to understand the construction and syntax - Mavar
  • @andreymal, the answers - in the answers. - Qwertiy
  • @Qwertiy oh everything - andreymal

1 answer 1

Parsing date to datetime object (time in this object will be 0 hours without a specific time zone, but it does not matter to us):

 >>> d = datetime.datetime.strptime('20170703', '%Y%m%d') >>> print(d) 2017-07-03 00:00:00 >>> type(d) <class 'datetime.datetime'> 

Call the special isoweekday method on this object:

 >>> print(d.isoweekday()) 1 

1..6 - Monday .. Saturday, respectively, 7 - Sunday (sometimes Sunday is zero, but according to the documentation, not on isoweekday)