My code isn't the cleanest or most optimized, but I'm pretty sure it works.
def main():
day = 4000
months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
while True:
try:
oldDate = (input("Date: ").strip()).strip()
type = dateType(oldDate, months)
if type == "numbers":
parts = oldDate.split("/")
year = parts[2]
month = parts[0].zfill(2)
day = parts[1].zfill(2)
elif type == "name":
for c in range(len(months)):
if oldDate.split()[0].title() in months[c]:
month = c+1
month = str(month).zfill(2)
split = oldDate.replace(",", "").split()
year = split[2]
day = split[1].zfill(2)
if int(day) < 31:
print(year.strip(),month.strip(),day.strip(), sep ="-", end="")
print()
except EOFError:
print()
break
except ValueError:
pass
def dateType(oldDate, months):
if oldDate.split()[0].title() in months:
return "name"
elif isNumbers(oldDate):
return "numbers"
def isNumbers(oldDate):
parts = oldDate.split("/")
for c in parts:
for d in c:
if d.isalpha():
return False
return True
main()