You can convert to a datetime object using strptime
. From here, you can use the weekdays
method.
However, your conditions are inaccurate, if you only want to keep Monday through Friday. Note that weekday
returns an integer from 0 to 6 with Monday being 0 and Sunday being 6.
Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
This means you want to check if the returned value is 5
or 6
.
The conditional you have will not work for another reason.
if some_datetime_object.datetime.weekday() != 6 or 1:
This is saying if the weekday is NOT Sunday OR True. You are short circuiting your conditional and forcing everything to be True
. You need to make it something like this (with the appropriate values adjusted)
if some_datetime_object.datetime.weekday() != 6 and some_datetime_object.datetime.weekday() != 1:
I did this differently in the code below by using not in
if datetime.datetime.strptime(d, '%Y%m%d').weekday() not in (5,6):
import datetime
dates = [
"20150801",
"20150802",
"20150803",
"20150804",
"20150805",
"20150806",
"20150807",
"20150808",
"20150809",
]
for d in dates:
if datetime.datetime.strptime(d, '%Y%m%d').weekday() not in (5,6):
print d
This outputs:
20150803
20150804
20150805
20150806
20150807