【问题标题】:Best way to Figure out the next weekly schedule by specific day name按特定日期名称计算下一周时间表的最佳方法
【发布时间】:2011-04-11 10:42:28
【问题描述】:

假设我有一个名为NextSend 的属性代表DateTime 值

4/11/2011 10:30:00 AM - Monday

假设我有一个必须每周发送特定日期的时间表 (Monday) 在这种情况下,所以为了弄清楚下一周的时间表,我最终得到了解决方案我必须检查每个下一个日期的日期,直到 DayOfWeek 与日程表中的 specific day 匹配

4/17/2011 10:30:00 AM - Monday

还有其他最好的方法来克服检查每个第二天日期的日期名称吗?

这是我提到的逻辑:

  int nowYear = DateTime.Now.Year;
  int nowMonth = DateTime.Now.Month;
  int nowDay = DateTime.Now.Day;
  int scheduleHour = this.Schedule.Time.Value.Hour;
  int scheduleMinute = this.Schedule.Time.Value.Minute;
  int scheduleSecond = this.Schedule.Time.Value.Second;
  int scheduleDay = -1;
  if(this.Schedule.Day.HasValue)
      scheduleDay= this.Schedule.Day.Value;

 switch (type)
            {
                case Schedule.ScheduleType.Weekly:
                    bool founded = false;
                    while (!founded)
                    {
                        //Check if last day of the current month
                        if (DateTime.DaysInMonth(nowYear, nowMonth) == nowDay)
                        {
                            //last day at this year, then move to next year
                            if (nowMonth == 12)
                            {
                                nowYear++;
                                nowMonth = 1;
                                nowDay = 1;
                            }
                            //its the end of a month then Move to next month
                            else
                            {
                                nowMonth++;
                                nowDay = 1;
                            }
                        }
                        //Get new proposed date
                        newNextSend = new DateTime(nowYear, nowMonth, nowDay, scheduleHour, scheduleMinute, scheduleSecond);

                        //Check if Next week schedule founded with specific day name
                        if (newNextSend.DayOfWeek ==
                            (DayOfWeek)Enum.Parse(typeof(DayOfWeek), Schedule.daysCalendar[scheduleDay - 1]))
                        {
                            founded = true;
                        }
                        else
                            nowDay++;
                    }
                    break;
}

【问题讨论】:

    标签: c# datetime schedule


    【解决方案1】:

    正如所有其他人所说:AddDays(7) 将确保每周计划按要求运行。但是,如果您正在寻找一种方法来确定特定工作日的下一次出现,那么您可以执行以下操作:

    private static DateTime GetNextDayOccurrence(DayOfWeek day, DateTime startDate)
    {
        if (startDate.DayOfWeek == day)
        {
            return startDate;
        }
        else
        {
            return GetNextDayOccurrence(day, startDate.AddDays(1));
        }
    }
    

    然后这样的调用

    var nextWednesday = GetNextDayOccurrence(DayOfWeek.Wednesday, DateTime.Today);
    

    将返回星期三的第一次出现。之后每周计划继续运行。如果您需要从下周开始第一次出现“星期三”,请通过

    DateTime.Today.AddDays(7) 
    

    到上述方法的“startDate”参数。

    【讨论】:

    • 这就是我要找的,因为我要开始的 Date 可能与上一个 nextSend 不同,谢谢
    【解决方案2】:

    为什么不在原日期上增加 7 天?一周中的天数是恒定的;)

    下周一:

    var nextMonday = thisMonday.AddDays(7);
    

    【讨论】:

      【解决方案3】:
      1. 有一个方法调用 AddDays 可以自动处理月份和年份的换行。
      2. 每周总是 7 天。只需添加当前星期几与日程表中星期几之间的差异。

      【讨论】:

        【解决方案4】:

        你不能使用 AddDays(7) 吗?

        例如

        DateTime nextSheduleDate = scheduleDateTime.AddDays(7); 
        

        这是 Tobias 答案的非递归版本。使用丹尼尔提到的逻辑。

        public static DateTime GetNextDayOccurrence(DateTime startDate, DayOfWeek dayOfWeek)
        {
          var offset = startDate.DayOfWeek > dayOfWeek ? 7 : 0;
          var days = (int) dayOfWeek + offset - (int) startDate.DayOfWeek;
        
          var dateTime = startDate.AddDays(days);
          return dateTime;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多