我想知道是否有解决方案将 2020年12月4日(星期五)转换为正确的ISO 8601格式: 2020-12-04
我当前的解决方案是在这里解决这个问题
available_month = { "January": "01", "February": "02", "March": "03", "April": "04", "May": "05", "June": "06", "July": "07", "August": "08", "September": "09", "October": "10", "November": "11", "December": "12", }
date_input = "Friday, 04 December 2020"
separated_date = date_input.split()
day = int(separated_date[1])
month = int(available_month.get(separated_date[2]))
year = int(separated_date[3])
iso_date = date(year, day, month).isoformat()
print(iso_date)
我的新解决方案现在是这里的解决方案:
from datetime import datetime
DATE_FORMAT = "%A, %d %B %Y"
date_input = "Friday, 04 December 2020"
date = datetime.strptime(date_input, DATE_FORMAT).date()
iso_date = date.isoformat()