开发者

how to convert a simple python code weekday check

开发者 https://www.devze.com 2023-02-17 01:17 出处:网络
Okay, i wrote this code myself awhile ago but now im开发者_运维技巧 having trouble conerting it so it only does this years dates, ie the user would only have to type MM-DD instead of YYYY-MM-DD beca

Okay, i wrote this code myself awhile ago

but now im开发者_运维技巧 having trouble conerting it so it only does this years dates, ie the user would only have to type MM-DD instead of YYYY-MM-DD because we are going to assume its 2011


import datetime
def weekday(dateinput):
    a = dateinput.split("-")
    b = datetime.date(datetime.date.today().year, int(a[0]), int(a[1]))
    c = b.strftime("%A")
    return c
#- test harness: do not modify -#
dateinput = raw_input('Enter a date ("MM-DD"): ')
print "That's a " + weekday(dateinput) + '.'

If you actually want to assume 2011 even in 2012, replace datetime.date.today().year with the hard-coded year.


import datetime
def weekday(dateinput):
    dateinput = "2011-" + dateinput
    a = dateinput.split("-")
    b = datetime.date(int(a[0]), int(a[1]), int(a[2]))
    c = b.strftime("%A")
    return c

print weekday("03-15")

prints Tuesday

0

精彩评论

暂无评论...
验证码 换一张
取 消