Home > Software engineering >  Convert a specific date into fiscal year in python
Convert a specific date into fiscal year in python

Time:11-21

I am trying to classified a specific date by fiscal year in python. My code is as follows. As you can see, the date I want to convert is 5/10/2017 which is in 2017 fiscal year,

!pip install fiscalyear
import fiscalyear

fiscalyear.START_MONTH = 9

New_CF_Date='5/10/2017 12:00:00 AM'
blank_pos=New_CF_Date.index(' 12:00:00 AM')
New_CF_Date_0=New_CF_Date[:blank_pos]
format = "%m/%d/%Y"
New_CF_Date = datetime.datetime.strptime(New_CF_Date_0, format)

intTargetFiscalYear = fiscalyear.FiscalYear.New_CF_Date.fiscal_year

print(intTargetFiscalYear)

However, it keeps showing error. AttributeError: type object 'FiscalYear' has no attribute 'New_CF_Date' . Please help me to find the bug.

CodePudding user response:

The fiscalyear.FiscalDate() does not take the datetime.datetime object as an input, so you may want to explicitly create a new fiscalyear.FiscalDate object using the datetime object.

import datetime
import fiscalyear

fiscalyear.setup_fiscal_calendar(start_month=9)

input_date='5/10/2017'
New_CF_Date = datetime.datetime.strptime(input_date, "%m/%d/%Y")
fiscal_date = fiscalyear.FiscalDate(New_CF_Date.year, New_CF_Date.month, New_CF_Date.day)
print(fiscal_date)
print(fiscal_date.fiscal_year)

Output:

2017-05-10
2017
  • Related