How to get current and previous financial year Ruby?

1.8k Views Asked by At

How to get current and previous financial year datewise in ruby based on current date ?

current_month = Time.current.month

current_year = current_month > 3 ? Time.current.year :Time.current.year - 1

start_date = '/04/01'
end_date = '/03/31'

current_financial_year = [current_year.to_s +  start_date ,  (current_year + 1).to_s + end_date]

previous_financial_year = [(current_year-1).to_s + start_date, current_year.to_s + end_date]
2

There are 2 best solutions below

2
spickermann On

Financial years are defined differently in different countries (see: Wikipedia). In your country, the following might work. Keep in mind that you need to adjust the calculation when you want to support multiple jurisdictions.

def current_financial_year
  year_range(current_financial_year_start)
end

def previous_financial_year
  year_range(current_financial_year_start - 1.year)
end

private

def current_financial_year_start
  date = Date.today
  date.change(year: date.year - 1) if date.month < 4
  date.change(month: 4).beginning_of_month
end

def year_range(date)
  (date .. date + 1.year - 1.day)
end

Note that my methods return ranges instead of arrays which define the financial period.

0
Anand Jose On

The method will return fiscal year start ans end date

def get_fiscal_year_start(date)
  date = date.change(year: date.year - 1) if date.month < 4
  date.change(month: 4).beginning_of_month
end
 

def get_fiscal_year_end(date)
  date = date.change(year: date.year + 1) if date.month > 3
  date.change(month: 3).end_of_month
end