简体   繁体   中英

How to get the start and end of Fiscal Year in C# based on current date? - Using SSIS Script Task

I created an SSIS Package that reads through existing files and makes changes/moves based on the file names and sheets. I have a C# Script Task that a co-worker wrote which is manually updated for the start and end of the fiscal year ; however, we have others using this package so I would like to automate the Fiscal Year dates.

How can I get the starting and ending date of the current Fiscal Year based on today's date?

I did find Working out start of financial year and How to discover financial Year based on current datetime? however, I can't seem to get the top-voted answers to work. I'm using a Reference/Namespace of Microsoft.Office.Interop.Excel but I'm not sure if that makes a difference.

  • Fiscal Year 2021 is 09/01/2020 to 08/31/2021
  • Fiscal Year 2022 is 09/01/2021 to 08/31/2022
  • Fiscal Year 2023 is 09/01/2022 to 08/31/2023

Today is 08/19/2022, the current Fiscal Year is 09/01/2021 - 08/31/2022, but when we hit 09/01/2022, it will change to 09/01/2022 - 08/31/2023.

This is what I use now, calling FYStart and FYEnd multiple times.

public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{ 
    public void Main()
    {

       //Update Fiscal Year Here (YYY, MM, DD): 
       DateTime FYStart = new DateTime(2021, 09, 01);
       DateTime FYEnd = new DateTime(2022, 08, 31);

It sounds like you're saying that if the current month is greater than 8, then the fiscal year started in the previous year. Otherwise it started this year.

If so, this can be represented in code by:

public static int GetFinancialYearStart(DateTime input)
{
    return input.Month > 8 ? input.Year - 1 : input.Year;
}

Then we can just call this method to set our start and end dates::

int fyStartYear = GetFinancialYear(DateTime.Now);
DateTime fyStart = new DateTime(fyStartYear, 09, 01);
DateTime fyEnd = new DateTime(fyStartYear + 1, 08, 31);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM