Calculate Dynamic Current Calendar / Financial Year In C#

I need a financial / calendar year based on current or today's date time
We need some time dynamically financial year or calendar year base on current date.
Suppose any company or organization have calendar year like 1st Jan to 31th Dec, 1st Feb to 31th Jan, 1st Apr to 31 Mar, 1st Oct to 31th Sep etc...…
So we need calculate current calendar year base on calendar start month of organization /company.
Examples
Suppose Today's date is 5th Feb 2017 and organization calendar start month is Jan(01) then current calendar year we will be 1st Jan 2017 to 31th Dec 2017.
Suppose Today's date is 10th Mar 2017 and organization calendar start month is Apr(04) then current calendar year we will be 1st Apr 2016 to 31th Mar 2017.
Suppose Today's date is 10th May 2018 and organization calendar start month is Apr(04) then current calendar year we will be 1st Apr 2018 to 31th Mar 2019.
  1. public static DateTime GetCurrentCalendarStartDate(int startMonth)   
  2. {  
  3.     DateTime startDate, currentDate = DateTime.Now;  
  4.     startDate = currentDate.Month >= startMonth ? new DateTime(currentDate.Year, startMonth, 01) : new DateTime(currentDate.Year - 1, startMonth, 01);  
  5.     return startDate;  
  6. }  
  7. public static DateTime GetCurrentCalendarEndDate(DateTime calendarStartDate)   
  8. {  
  9.     return calendarStartDate.AddDays(-1).AddYears(1);  
  10. }  
I have created two methods for current calendar start date and end date.
First method “GetCurrentCalendarStartDate” take input organization calendar start month and return current calendar date.
Second method “GetCurrentCalendarEndDate” take calendar start month and return calendar end date.
Same concept we will use in for calculate financial year , start month April (04) then we will able to get current financial year 1st Apr to 31th Mar.

Comments

Popular posts from this blog

Form submit resulting in “InvalidDataException: Form value count limit 1024 exceeded.” In ASP.NET Core

Repository Design Pattern in C#