[SOLVED] Get fiscal year based on current timestamp-python


This Content is from Stack Overflow. Question asked by matt.aurelio

I saw the module fiscalyear and I read some similar questions:

Get fiscal year based on current timestamp-python

However, I am still unable to get the right answer.

My aim is to return the fiscal year based on the month that we are currently in as an integer.

import fiscalyear
import datetime

fiscalyear.START_MONTH = 6
cur_y = fiscalyear.FiscalYear(datetime.datetime.now().year)


>> FY2022

# Today is 18th September 2022, it should return FY2023

How can I return the actual fiscal year as an integer if the fiscal year starts in June?

So, June 2022-May 2023 is actually Fiscal Year 2023.



My knowledge in the fiscal area is somewhere close to 0, but according to [ReadTheDocs]: fiscalyear Documentation:

The FiscalYear class provides an object for storing information about the start and end of a particular fiscal year.

The start and end of each quarter are stored as instances of the FiscalDateTime class. This class provides all of
the same features as the datetime class, with the addition of the ability to query the fiscal year and quarter

So, FiscalYear isn’t to be used with dates, but FiscalDateTime (or its simpler sibling: FiscalDate) instead.

>>> import fiscalyear
>>> fiscalyear.START_MONTH = 7
>>> cur_y = fiscalyear.FiscalYear(datetime.datetime.now().year)
>>> cur_y.start.date()
datetime.date(2018, 7, 1)
>>> cur_y.end.date()
datetime.date(2019, 6, 30)
>>> cur_dt = fiscalyear.FiscalDate(2019, 2, 19)  # Current date (at answer time)
>>> cur_dt.fiscal_year
>>> jul_dt = fiscalyear.FiscalDate(2019, 7, 19)  # A date past July 1st (when the fiscal year should change)
>>> jul_dt.fiscal_year

This Question was asked in StackOverflow by anky and Answered by CristiFati It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?