Skip to content

Instantly share code, notes, and snippets.

@westc
Created May 31, 2018 03:25
Show Gist options
  • Save westc/55314af02bc5a5551f3cb64fb5c07c93 to your computer and use it in GitHub Desktop.
Save westc/55314af02bc5a5551f3cb64fb5c07c93 to your computer and use it in GitHub Desktop.
Calculate the fiscal year and fiscal quarter easily in JavaScript. Definitions can be reduced to one liners.
function fiscalQuarter(date, opt_monthStart /* default: 9 (October) */) {
opt_monthStart = opt_monthStart == undefined ? 9 : opt_monthStart;
var month = date.getMonth(),
monthIndex = ((month - opt_monthStart) + 12) % 12;
return ~~(monthIndex / 3) + 1;
}
function fiscalYear(date, opt_monthStart /* default: 9 (October) */) {
opt_monthStart = opt_monthStart == undefined ? 9 : opt_monthStart;
var month = date.getMonth(),
year = date.getFullYear(),
yearOffset = Math.floor((month - ((opt_monthStart % 12) || 12)) / 12) + 1;
return yearOffset + year;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment