Introduction
At work, I'm writing code that imports fiscal data from a series of spreadsheets. In this case, I'm retrieving fiscal month/year as opposed to the calendar month/year. This means I had to convert the specified date. For instance, the US government uses October as the beginning of the fiscal year, so some minor math was involved. This extension method should work for any month you might happen to use.
The Code
This extension method will convert the specified date to the appropriate fiscal date:
public static class ExtendDateTime
{
public static DateTime FiscalDate(this DateTime thisDate, int fiscalStartMonth)
{
return thisDate.AddMonths(13 - fiscalStartMonth);
}
}
Using the code
Usage is pretty simple. In the example below, I'm using October for the beginning of the fiscal year:
DateTime fiscalDate = DateTime.Now.FiscalDate(10).Date;
History
27 Oct 2014 - Original posting.
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.