Never underestimate the power of Passion!

Tuesday 7 February 2017

On 21:29 by Vardan Kumar in ,    No comments
Dynamic financial Year

Dynamic financial year date

Many of us come across setting up financial year date dynamically in our code, the reason might be fetching values from your database between one financial year, more precisely the current financial year or we might want to display a header somewhere on your console or application what so ever it may be. The reason or the programming language we intend could be anyone's guess but the logic will be same.

Now everyone would have different start and end date of a financial year, i.e. the date value as well as month value can be hard coded, it would be better if we use macros or const. variable for the same so that if we want to change them at any point of time, we won't need to go through the whole code, we'll just update at the definition of macro or const variable. So what left is yet to be identified and can not be hard coded is the "year". 

Let us try to understand it with an example........

Suppose for fiscal year 2017 let us suppose our dates are  01-Jul-2016 to 30-Jun-2017.

So 01-Jul and 30-Jun can be hard coded but we need to identify the current year variation. Lets generalize it. First of all we have to find out today's date

As you generalize the pattern ,if the month value of today's date is less than or equal to '6' then the start date will be 01-Jul-"(Today's date's year value)-1" and  the end date will be 30-Jun-"Today's Date's year value" else start date will be 01-Jul-"Today's Date's year" and end date will be 30-Jun-"Today's date year value +1". You might have a clear idea of the logic, if not you ll be having in just couple of minutes by looking at the source code below.

Dynamic Financial Year C#
Dynamic Financial year

C# Source code for dynamic financial year or fiscal year date

int fy=0;
String start_date, end_date;
DateTime cd = DateTime.Today;   //Fetching system's date
                int yr = cd.Year;             //Parsing year from today's date
                if (cd.Month <= 6)         
                    start_date = "01-JUL-" + (yr - 1).ToString();  //
                    end_Date = "30-JUN-" + (yr).ToString();
                    fy = yr;
                    start_date = "01-JUL-" + yr.ToString();
                    end_date = "30-JUN-" + (yr + 1).ToString();
                    fy = yr + 1;

Now since you have the start date and end date strored in the variables we can use them for whatever reason we intend to. Also we can use same logic in any programming language we want just we need to change the syntax. And don't forget to change the fiscal year's date and month value according to your requirement.

Use of macros or const. variables are recommended for the purpose of hard coding the dates.


Post a Comment