python - I'm unable to get date with a variable -
def get_next_monday(year, month, day): date0 = datetime.date(year, month, day) next_monday = date0 + datetime.timedelta(7 - date0.weekday() or 7) return next_monday date2 = datetime.datetime.now().strftime("%y, %m, %d") findnextweek = get_next_monday(date2)
if replace (year, month, day)
(date2)
integer required. otherwise, different error
typeerror: get_next_monday() takes 3 arguments (1 given)
you have little problems in code. here's fix , explanation:
import datetime def get_next_monday(year, month, day): # if want accept str , int, must convert strings date0 = datetime.date(int(year), int(month), int(day)) next_monday = date0 + datetime.timedelta(7 - date0.weekday() or 7) return next_monday # must convert date in list, because `get_next_monday` takes 3 arguments. initial_date = datetime.datetime.now().strftime("%y, %m, %d").split(',') # must un-pack date findnextweek = get_next_monday(*initial_date) print(findnextweek)
note should calling get_next_monday
get_next_monday(2016, 6, 10)
or get_next_monday('2016', '6', '10')
.
there's no sense of creating datetime object, converting in string, list, , re-converting in datetime object.
anyways hope :)
Comments
Post a Comment