如何使用日期在python中显示财务月和年。
伙计们请帮助我......提前致谢
答案 0 :(得分:0)
import datetime
#function take input of the datestring like 2017-05-01
def get_financial_year(datestring):
date = datetime.datetime.strptime(datestring, "%Y-%m-%d").date()
#initialize the current year
year_of_date=date.year
#initialize the current financial year start date
financial_year_start_date = datetime.datetime.strptime(str(year_of_date)+"-04-01","%Y-%m-%d").date()
if date<financial_year_start_date:
return 'April, '+ str(financial_year_start_date.year-1)+' to March, '+ str(financial_year_start_date.year)
else:
return 'April, '+ str(financial_year_start_date.year)+' to March, '+ str(financial_year_start_date.year+1)
<强>输出强>
get_financial_year("2017-05-01") : April, 2017 to March, 2018