2

ValueNextSendを表すというプロパティがあるとします。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;
}
4

4 に答える 4

4

他のすべての人々も同様に言っているように: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」パラメータに変更します。

于 2011-04-11T11:35:14.543 に答える
2

元の日付に 7 日を追加しないのはなぜですか? 1 週間の日数は一定です ;)

次の月曜日:

var nextMonday = thisMonday.AddDays(7);
于 2011-04-11T10:45:17.897 に答える
0

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;
}
于 2011-04-11T10:47:44.410 に答える
0
  1. AddDays月と年のラップを自動的に処理するメソッド呼び出しがあります。
  2. 週は常に 7 日間です。現在の曜日とスケジュールの曜日の差を追加するだけです。
于 2011-04-11T10:47:50.700 に答える