This can be useful when you need to print current session year or, current fiscal/financial year somewhere in your page and specifically when you do not want to invoke a server method for it.
Defining Function
<script language="javascript"> function getCurrentFiscalYear() { //get current date var today = new Date(); //get current month var curMonth = today.getMonth(); var fiscalYr = ""; if (curMonth > 3) { // var nextYr1 = (today.getFullYear() + 1).toString(); fiscalYr = today.getFullYear().toString() + "-" + nextYr1.charAt(2) + nextYr1.charAt(3); } else { var nextYr2 = today.getFullYear().toString(); fiscalYr = (today.getFullYear() - 1).toString() + "-" + nextYr2.charAt(2) + nextYr2.charAt(3); } document.write(fiscalYr); } </script>
Calling Function
<script language="javascript">getCurSession()</script>
Note:
- I have made the function as per Indian fiscal year. You can change it according to your country. You only need to modify the conditional statement i.e, if (curMonth > 3) where April(3) is the starting month of fiscal year.
- getMonth() returns number of the month in a date value starting from 0 to 11
- You can change this function to return the session year which can be input for another logic/function. Accordingly you have to change the function.
Good Job…..Keep It up buddy….
Thanks buddy for the compliment
Great, Thanks
Simple than ur code
————————————
from_date = new Date(today.getFullYear() – 1, 3, 1, 0, 0, 0, 0);
to_date = new Date(today.getFullYear(), 2, 31, 0, 0, 0, 0);
Suvendu,
Great code; it is very useful. Just one thing: for your case of the fiscal year starting in April, shouldn’t the conditional “if (curMonth > 3)” be “if (curMonth >= 3)” [or, perhaps “if (curMonth > 2)”]? The months are zero-based, I understand, so April would be 3, but using the > operator means the fiscal year would have to be greater than 3 (April), therefore the fiscal year would start in May.