Skip to content

Instantly share code, notes, and snippets.

@connect-dips
Created October 21, 2019 15:58
Show Gist options
  • Save connect-dips/90bbcfd4cea02a37ce8b5c8e0e65d7d5 to your computer and use it in GitHub Desktop.
Save connect-dips/90bbcfd4cea02a37ce8b5c8e0e65d7d5 to your computer and use it in GitHub Desktop.
A javascript method using momentjs to get determine the current financial year.
let fy = getFinancialYear();
$('#displayFy').text('Financial year is: ' + fy);
$( "input#datePicker" ).change(function() {
fy = getFinancialYear($(this).val());
$('#displayFy').text('Financial year is: ' + fy);
});
function getFinancialYear(date) {
let financialYear;
let today = moment();
if(date){
today = moment(date, 'YYYY-MM-DD');
}
if(today.month() >= 3){
financialYear = today.format('YYYY') + '-' + today.add(1, 'years').format('YYYY')
}
else{
financialYear = today.subtract(1, 'years').format('YYYY') + '-' + today.add(1, 'years').format('YYYY')
}
return financialYear
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment