+ 6
Day of the week challenge
You receive a date and need to know what day of the week it is. Task: Create a program that takes in a string containing a date, and outputs the day of the week. Input Format: A string containing a date in either "MM/DD/YYYY" format or "Month Day, Year" format. Output Format: A string containing the day of the week from the provided date. Sample Input: 11/19/2019 Sample Output: Tuesday https://code.sololearn.com/c8TxWYHQOh0A/?ref=app My code works only for the first format of input. I am struggling to convert string from "Month Day, Year" (for example "July 20, 2020") format to date format . Any thoughts?
2 Respuestas
+ 5
# Try it %B Month as locale’s full name
else:
dateform = datetime.strptime(date, '%B %d, %Y')
https://strftime.org
0
import datetime
#Converts Month into number(ex: January --> 1)
def mConv(dats):
months={
'january':1,
'february':2,
'march':3,
'april':4,
'may':5,
'june':6,
'july':7,
'august':8,
'september':9,
'october':10,
'november':11,
'december':12
}
dats[0]=str(months[dats[0].lower()])
return dats
#Gets the weekday
def getD(date):
wDays={
0:"Monday",
1:"Tuesday",
2:"Wednesday",
3:"Thursday",
4:"Friday",
5:"Saturday",
6:"Sunday"
}
date=datetime.datetime(int(date[2]),int(date[0]),int(date[1]))
return wDays[date.weekday()]
d=input()
#For MM/DD/YYYY
if d[0].isnumeric():
d = d.split("/")
print(getD(d))
#For Month Day, Year
elif d[0].isalpha():
d = d.split(' ')
d[1]=d[1].replace(",","")
print(getD(mConv(d)))
Here is my simple but long solution