4
votes

Is there a way to find out years and quarters based on a date range.

We are considering November as Start date and October as End date of financial year.

Ex: Nov prev Year to Oct next Year is considered as one financial year.

November 1, 2014 - October 31, 2015 - > It is considered as one financial year

Any record past Oct 31 will go to next financial year.

We also need to find Quarters as below

Quarter 1 = November, December & January
Quarter 2 = February, March, April
Quarter 3 = May, June, July
Quarter 4 = August, September, October

moment('2014-12-01').utc().quarter()->
it returns 4 considering Jan as Start date of financial year.

But it is considered as 1st quarter.

-- Code --

function getQuarter() { var obj = {};

 // Set Quarters form nov
 obj.quarter1 = {start:Moment().month(10).startOf('month'),end:Moment().month(0).endOf('month').add(1,'years')};
 obj.quarter2 = {start:Moment().month(1).startOf('month').add(1,'years'),end:Moment().month(3).endOf('month').add(1,'years')};
 obj.quarter3 = {start:Moment().month(4).startOf('month').add(1,'years'),end:Moment().month(6).endOf('month').add(1,'years')};
 obj.quarter4 = {start:Moment().month(7).startOf('month').add(1,'years'),end:Moment().month(9).endOf('month').add(1,'years')};

 return obj;

}

have used this to set quarters starting from November.

Is there any alternative approach ?

1

1 Answers

2
votes

I cannot think of any built in functions or libraries that allow a year to span across 2 years. You could easily write a FinacialYear object that does this though, and use it as a wrapper around the Date object. You would need a constructor that takes in a start year or end year, and bases all the calculations on that.

then just overload the toString() method to return "2014-2015", and a getQuarter(date) which uses a if/else to return 1...4.