Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#5.0

Determine Fiscal Date

4.82/5 (5 votes)
27 Oct 2014CPOL 15.4K  
Extension method to determine fiscal date

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:

c#"
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:

C#
DateTime fiscalDate = DateTime.Now.FiscalDate(10).Date;

History

27 Oct 2014 - Original posting.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)