Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C# 5.0
Tip/Trick

Determine Fiscal Date

Rate me:
Please Sign up or sign in to vote.
4.82/5 (5 votes)
27 Oct 2014CPOL 14.7K   4   3
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)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
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.

Comments and Discussions

 
QuestionFiscalYear Pin
Peter Heutink17-Dec-14 4:06
Peter Heutink17-Dec-14 4:06 
QuestionI think I'm missing something Pin
lespauled27-Oct-14 8:22
lespauled27-Oct-14 8:22 
I know it's probably just me not being able to wrap my head around it, but I can't see how this would work.

Say I have a date, 12/12/2014 and I run your extension method, it adds 1 month. I would get 1/12/2015 as the fiscal date. And if I have 11/12/2014, it adds 2 months, and I also get 1/12/2015.

What does 1/12/2015 have to do with the fiscal date (in this case)?
AnswerRe: I think I'm missing something Pin
#realJSOP27-Oct-14 8:56
mve#realJSOP27-Oct-14 8:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.