0

I have a dataset that I need to remove all records that are outside of the current financial year. I have a function that returns a boolean that suggests if the record will be deleted or not, for some reason it is telling me to delete records that are inside the current financial year... Maybe a logic issue?

The Function:

public static Boolean GetCurrentFinancialYear(DateTime CompareDate)
{
    TimeSpan diff = DateTime.Now - CompareDate;
    double Years = diff.TotalDays / 365.25;
    double Months = diff.TotalDays / 12;
    Boolean DeleteRecord = false;
    var CurrentYear = DateTime.Today.Year;
    var CurrentMonth = DateTime.Today.Month;

    if(CompareDate.Month == 10 && CompareDate.Year == 2017)
    {
        var something = "nothing";
    }

    if (Months >= 13)
    {
        /// This is over a year old, safe bet its last finacial year or earlier
        /// 
        DeleteRecord = true;
    }
    else
    {
        var CurrentEndDate = new DateTime(DateTime.Now.Year, 7, 1);
        var CurrentStartDate = new DateTime(DateTime.Now.Year - 1, 6, 30);
        TimeSpan Enddiff = CurrentEndDate - CompareDate;
        TimeSpan Startdiff = CurrentStartDate - CompareDate;
        double EndMonths = Enddiff.TotalDays / 12;
        double StartMonths = Startdiff.TotalDays / 12;

       if (EndMonths < 12 && StartMonths > 0)
        {
            DeleteRecord = false;

        }
        else
        {
            DeleteRecord = true;
        }


    }
    return DeleteRecord;
}
Caz1224
  • 1,539
  • 1
  • 14
  • 37
  • 1
    Possible duplicate of [How to know if a DateTime is between a DateRange in C#](https://stackoverflow.com/questions/4781611/how-to-know-if-a-datetime-is-between-a-daterange-in-c-sharp) – Sebastian L Mar 14 '18 at 09:04

1 Answers1

0

Found an extremely simple and elegant solution

public static Boolean GetCurrentFinancialYear(DateTime CompareDate)
{
    Boolean DeleteRecord = false;

    var CurrentEndDate = new DateTime(DateTime.Now.Year, 7, 1);
    var CurrentStartDate = new DateTime(DateTime.Now.Year - 1, 6, 30);

    if (CompareDate >= CurrentStartDate && CompareDate < CurrentEndDate)
    {
        DeleteRecord = false;
    }
    else
    {
        DeleteRecord = true;
    }      
    return DeleteRecord;
}
Caz1224
  • 1,539
  • 1
  • 14
  • 37