Get fiscal quarter from month number

Viewed 46

I'd like to create a function to take the current month number (1 to 12) and produce a fiscal quarter i.e. 1, 2, 3 or 4.

A lot of the examples I have found use case statements but this seems over engineered for what I feel should be achievable using math.

I have tried a few different methods but have fallen a little short such as:

function getQuarter(m) { return round(m/4) }

Edit: building on @neil answer the function can be further shortened to:

function getQuarter(m) { return Math.ceil(m/3) }
1 Answers
function getQuarter(m) { return Math.floor((m - 1)/3) + 1 }

You subtract 1, because January isn't 1 month into the new year, it's the first month. Likewise 12 isn't a new quarter, it's the last month of the last quarter.

Divide by 3 instead of 4 (3 months to a quarter) and floor it. Then add one and the returned value is a number from 1 to 4 indicating which quarter you're in.

Good luck!

Related