C#类选择当前季度而不是上一季度的日期

时间:2011-04-04 10:23:03

标签: c# asp.net datetime

我有一个C#类作为销售报告引擎。所以基本上它从数据库中获取数据取决于用户选择的日期范围。

用户可以通过两种方式选择日期 - 按特定日期范围或按季度(即当前季度,上一季度)。

当系统交给我们时,上一季度选项功能正常,但现在它只抓住与当前季度相同的日期(我不知道为什么,此部分的源代码没有在全部)。

我一直在寻找它,试图弄清楚发生了什么,因为正如我之前说过它确实正常运作。我希望一副新鲜的眼睛可能有帮助吗?我通过代码进行了调试,当我选择GetPreviousQuarter时,它只计算GetCurrentQuarterDates。

以下是当用户选择按季度查看信息时调用的switch语句:

switch (reportRequest.SelectedPeriod)
{
    case SalesReportRequest.SalesReportPeriod.Today: // Today
        startDate = DateTime.Today;
        endDate = DateTime.Today.Add(new TimeSpan(0, 23, 59, 59, 59));
        break;
    case SalesReportRequest.SalesReportPeriod.Yesterday:
        startDate = DateTime.Today.AddDays(-1);
        endDate = DateTime.Today.AddDays(-1).Add(new TimeSpan(0, 23, 59, 59, 59));
        break;
    case SalesReportRequest.SalesReportPeriod.Last7Days:
        ReportDateRange dr3 = GetDatesOfLastSevenDays();
        startDate = dr3.StartDate;
        endDate = dr3.EndDate;
        break;
    case SalesReportRequest.SalesReportPeriod.CurrentQuarter: // Current Quarter.
        ReportDateRange dr = GetDatesOfCurrentQuarter();
        startDate = dr.StartDate;
        endDate = dr.EndDate;
        break;
    case SalesReportRequest.SalesReportPeriod.PreviousQuarter: // Previous Quarter.
        ReportDateRange dr1 = GetDatesOfPreviousQuarter();
        startDate = dr1.StartDate;
        endDate = dr1.EndDate;
        break;
    case SalesReportRequest.SalesReportPeriod.CurrentYear: // Current Year.
        ReportDateRange dr2 = GetDatesOfCurrentYear();
        startDate = dr2.StartDate;
        endDate = dr2.EndDate;
        break;
}

那么当选择Previous Quarter选项时,会调用GetDatesOfPreviousQuarter():

public ReportDateRange GetDatesOfPreviousQuarter()
{
    var dtmValue = DateTime.Now;
    return DatesOfQuarter(dtmValue);
}

然后调用DatesOfQuarter:

public ReportDateRange DatesOfQuarter(DateTime dtmValue)
{
    var intQuarter = (int)Math.Ceiling(dtmValue.Month / 3M);

    var intLastMonthOfQuarter = intQuarter * 3; 
    var intFirstMonthOfQuarter = intLastMonthOfQuarter - 2;
    var intLastDayOfQuarter = DateTime.DaysInMonth(dtmValue.Year, intLastMonthOfQuarter);

    ReportDateRange dateRange = new ReportDateRange
    {
        StartDate = new DateTime(dtmValue.Year, intFirstMonthOfQuarter, 1),
        EndDate = new DateTime(dtmValue.Year, intLastMonthOfQuarter, intLastDayOfQuarter)
    };

    return dateRange;
}

感谢任何帮助,如果需要,我可以提供更多信息。

感谢。

编辑:

仅仅是因为有人要求它:

    public ReportDateRange GetDatesOfCurrentQuarter()
    {
        var dtmValue = DateTime.Now;
        return DatesOfQuarter(dtmValue);
    }

3 个答案:

答案 0 :(得分:1)

您还可以发布GetDatesOfCurrentQuarter的代码吗?

我的猜测是,一个简单的修复可能会改变:

public ReportDateRange GetDatesOfPreviousQuarter()
{
    var dtmValue = DateTime.Now;
    return DatesOfQuarter(dtmValue);
}

public ReportDateRange GetDatesOfPreviousQuarter()
{
    var dtmValue = DateTime.Now.AddMonths(-3);
    return DatesOfQuarter(dtmValue);
}

答案 1 :(得分:1)

编辑:在您编辑的问题中,现在很明显GetDatesOfCurrenQuarter()方法和GetDatesOfPreviousQuarter()方法完全相同。

问题是GetDatesOfPreviousQuarter()方法错误地使用当前日期调用DatesOfQuarter()方法。

您的季度计算是正确的。 M正确地将分红标识为天花板计算的小数。 Ceiling计算用于“将结果向上舍入”为整数。随后将其转换为日期时间所属的正确Quarter的int值。

换句话说:DatesOfQuarter方法将正确返回一个结构,其中包含所提供日期所属的季度的开始日期和结束日期。

我认为您的问题的解决方案是使用以下两种方法:

 public ReportDateRange GetDatesOfCurrentQuarter() 
 {     
      var dtmValue = DateTime.Now;     
      return DatesOfQuarter(dtmValue); 
 } 

 public ReportDateRange GetDatesOfPreviousQuarter() 
 {     
      var dtmValue = DateTime.Now;     
      return DatesOfQuarter(dtmValue.AddMonths(-3)); 
 } 

SO社区无法确定此代码是否真的有效,或者这是否是最近由一位设法隐藏其踪迹的同事引入的错误(目前由于逻辑错误而拒绝参与)< / p>

答案 2 :(得分:0)

article包括各种日历周期的课程,如周,月,季,半年:

// ----------------------------------------------------------------------
public void CalendarYearTimePeriodsSample()
{
  DateTime moment = new DateTime( 2011, 8, 15 );
  Console.WriteLine( "Calendar Periods of {0}:", moment.ToShortDateString() );
  // > Calendar Periods of 15.08.2011:
  Console.WriteLine( "Year     : {0}", new Year( moment ) );
  Console.WriteLine( "Halfyear : {0}", new Halfyear( moment ) );
  Console.WriteLine( "Quarter  : {0}", new Quarter( moment ) );
  Console.WriteLine( "Month    : {0}", new Month( moment ) );
  Console.WriteLine( "Week     : {0}", new Week( moment ) );
  Console.WriteLine( "Day      : {0}", new Day( moment ) );
  Console.WriteLine( "Hour     : {0}", new Hour( moment ) );
  // > Year     : 2011; 01.01.2011 - 31.12.2011 | 364.23:59
  // > Halfyear : HY2 2011; 01.07.2011 - 31.12.2011 | 183.23:59
  // > Quarter  : Q3 2011; 01.07.2011 - 30.09.2011 | 91.23:59
  // > Month    : August 2011; 01.08.2011 - 31.08.2011 | 30.23:59
  // > Week     : w/c 33 2011; 15.08.2011 - 21.08.2011 | 6.23:59
  // > Day      : Montag; 15.08.2011 - 15.08.2011 | 0.23:59
  // > Hour     : 15.08.2011; 00:00 - 00:59 | 0.00:59
} // CalendarYearTimePeriodsSample

计算基于.NET CultureInfo.Calendar 。任何期间都提供 GetNextXxx GetPreviousXxx 方法:

// ----------------------------------------------------------------------
public void GetDaysOfPastQuarter( DateTime moment,
       out DateTime firstDay, out DateTime lastDay )
{
  TimeCalendar calendar = new TimeCalendar(
    new TimeCalendarConfig { YearBaseMonth = YearMonth.October } );
  Quarter quarter = new Quarter( moment, calendar );
  Quarter pastQuarter = quarter.GetPreviousQuarter();

  firstDay = pastQuarter.FirstDayStart;
  lastDay = pastQuarter.LastDayStart;
} // GetDaysOfPastQuarter