【发布时间】:2021-02-28 05:54:27
【问题描述】:
我必须在不使用datetime 的情况下返回给定字符串日期(例如“2019 年 9 月 14 日”)的年份 (int)。我还必须使它足够基本,以便我可以为不同的日历制作另外两个类似的功能。
这是我之前的:
def date_to_day_of_year(self, month, day, year):
'''
Given the month, day and year of a date,
return the number of the day in the year.
'''
day_of_year = 0
for month_name, days_in_month in month_days.items():
if month_name == month:
break
day_of_year += days_in_month
day_of_year += day
if self.is_leap_year(year) and ((month != 'Feb') and (month != 'Jan')):
day_of_year += 1
return day_of_year(month, day, year)
但我似乎无法将其转换为:
def day_of_year_to_date(self, day_of_year, year):
'''
Convert day of year to date.
'''
# ...
【问题讨论】:
标签: python date date-arithmetic days