Created
May 31, 2018 03:25
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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