通过特定的日期名称找出下一周的时间表的最佳方式 [英] Best way to Figure out the next weekly schedule by specific day name

查看:114
本文介绍了通过特定的日期名称找出下一周的时间表的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象我有一个名为 NextSend 的财产代表 DateTime 价值

imagine that i have a property called NextSend representing DateTime Value

4/11/2011 10:30:00 AM - 星期一

让我说在这种情况下,每周必须发送一个特定日期 (星期一) 的时间表,所以为了找出下一周的时间表结束了解决方案,我必须检查每一个下一个日期,直到 DayOfWeek 匹配具体日在计划

lets say that I have a schedule which must be sent every week for specific day (Monday) in this case, so in order to figure out the next weekly schedule I ended up with the solution where I have to check every next date's day till the DayOfWeek Matches the specific day in the schedule

4 / 17 / 2011 10:30: 00 AM - 星期一

有没有其他最好的方法来克服检查每个日期的日期名称?

Is there any other best way to overcome checking each next day date's day name?

这是我提到的逻辑:

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


推荐答案

所有其他人都也说:AddDays(7)将确保每周的时间表按需运行。但是,如果您想找出下一次特定工作日的方法,那么您可以执行以下操作:

As all the other folks have said as well: AddDays(7) will ensure the weekly schedule runs as required. But if you're looking for a way to figure out the next occurrence of a specific weekday, then you can do something like this:

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

将返回星期三的第一次出现。之后,每周计划进行运行。如果您需要下周第一次出现星期三,只需通过

will return the first occurrence of Wednesday. After which the weekly schedule carries on running. If you need the first occurrence of "Wednesday" as of next week, just pass

DateTime.Today.AddDays(7) 

到上述方法的startDate参数。

to the "startDate" parameter of the above method.

这篇关于通过特定的日期名称找出下一周的时间表的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆