1

I have this simple code to determine if the current date is part of the Fiscal Year 2014 or not. It sets a range between October 1, 2013 to September 30, 2014. Any date in that range will be considered Fiscal Year 2014:

var FYFirst     = 2013,
    FYLast      = 2014,
    beginningFY = new Date(FYFirst,9,1), //October 1
    endFY       = new Date(FYLast,8,30), //September 30
    todayDate   = new Date();
if (todayDate > beginningFY && todayDate < endFY ) { alert("FY 2014"); }
else { alert("Not this FY"); }

I would like to dynamically change FYFirst and FYLast. The way I wrote it, every October I would have to come and change it manually. I would appreciate any new ideas. thanks!

3 Answers 3

3

All you need to do to get the starting year is to go back one from the current year whenever the current month is earlier in the year than October:

var todayDate = new Date();
var FYFirst = todayDate.getFullYear(); 
if (todayDate.getMonth() < 9) {
    FYFirst -= 1;
}
var FYLast = FYFirst + 1;

If it's after October, then the starting year is the current year. You can also do this in one line, but I'm not sure I'd advise it:

var FYFirst = todayDate.getFullYear() - (todayDate.getMonth() < 9);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to lwburk, Yogesh and Jeremy for your suggestions, they all answer my question one way or another. At the end I decided to go with lwburk suggestions witha small modification: var todayDate = new Date(), FYFirst = todayDate.getFullYear(); if (todayDate.getMonth() > 9){FYFirst += 1;} var FYLast = FYFirst - 1; Thanks!
1

This will get you the current Fiscal year.

var today = new Date();
var currentYear = today.getFullYear();
var currentMonth = today.getMonth();
var FYFirst, FYLast;
if(currentMonth < 9)
{
    FYFirst = currentYear-1;
    FYLast = currentYear;
}
else
{
    FYFirst = currentYear;
    FYLast = currentYear + 1;
}
console.log(FYFirst);

jsfiddle

Comments

0

I believe this will do what you need for any arbitrary year:

var todayDate = new Date(),
    FYFirst,
    FYLast,
    beginningFY,
    endFY;
if (todayDate.getMonth() < 9) {
    FYFirst = todayDate.getFullYear()-1;
} else {
    FYFirst = todayDate.getFullYear();
}
FYLast = FYFirst + 1;
beginningFY = new Date(FYFirst,9,1); //October 1
endFY = new Date(FYLast,8,30); //September 30
if (todayDate > beginningFY && todayDate < endFY ){
    alert("FY " + FYFirst);
} else {
    alert("Not this FY");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.