Untitled

mail@pastecode.io avatar
unknown
python
9 days ago
1.4 kB
2
Indexable
Never
months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

def iso8601():
    while True:
        date = input("Date: ")

        if "/" in date:
            try:
                month, day, year = date.split('/')
                month = int(month)
                day = int(day)
                year = int(year)

                if (1 <= month <= 12) and (1 <= day <= 31):
                    break
                else:
                    raise ValueError
            except ValueError:
                pass

        elif "," in date:
            try:
                part1 = date.replace(',', ' ')
                part2 = part1.strip()
                mmddyyyy = part2.split()

                if len(mmddyyyy) == 3:
                    month_str, day, year = mmddyyyy
                    if month_str in months:
                        month = months.index(month_str) + 1
                        day = int(day)
                        year = int(year)
                        if 1 <= day <= 31:
                            break
                        else:
                            raise ValueError
                    else:
                        raise ValueError
            except ValueError:
                pass      
        else:
            pass

    return f"{year}-{month:02}-{day:02}"

print(iso8601())
Leave a Comment