制定财政年度记录

时间:2018-03-14 08:54:35

标签: c#

我有一个数据集,我需要删除当前财政年度之外的所有记录。 我有一个函数返回一个布尔值,建议是否删除记录,由于某种原因,它告诉我删除当前财政年度内的记录......可能是一个逻辑问题?

功能:

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;
}

1 个答案:

答案 0 :(得分:0)

找到一个非常简单而优雅的解决方案

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;
}