指定是提前几周还是提前几天

时间:2019-07-14 22:48:33

标签: c# datetime

我已经制定了一种方法,该方法必须有助于判断应该提前几周还是几天。 我有一个参数可以告诉它是否应该提前一周。

这是 x周数

public static IEnumerable<(DateTime start, DateTime end)> GetTimes(DateTime startTime, DateTime endTime, DayOfWeek startDay, int countWeeks)
            {
                TimeSpan diff = endTime - startTime;
                int daysUntilWeekDay = ((int)startDay - (int)startTime.DayOfWeek + 7) % 7;
                DateTime beginningDate = startTime.AddDays(daysUntilWeekDay);
                for (int i = 1; i <= countWeeks; i++)
                {
                    DateTime date = beginningDate.AddDays(7 * i);
                    yield return (start: date, end: date.Add(diff));
                }
            }

但是我的参数必须告诉它是否应该为Weeksforward。如果正确,那么它必须运行数周,如果错误,则今天应该起飞,并期待着它。

这必须既要提前一周又要提前几天。

public static IEnumerable<(DateTime start, DateTime end)> GetTimes(DateTime startTime, DateTime endTime, DayOfWeek startDay, int countWeeks, bool WeeksForward)
        {
            TimeSpan diff = endTime - startTime;
            //have i try to make "resultat"
            int resultat = (WeeksForward == false) ? (((int)startDay - (int)startTime.Day + 7) % 7) : (((int)startDay - (int)startTime.DayOfWeek + 7) % 7);
            int daysUntilWeekDay = resultat;
            DateTime beginningDate = startTime.AddDays(daysUntilWeekDay);
            for (int i = 0; i <= countWeeks; i++)
            {
                DateTime date = beginningDate.AddDays(7 * i);
                yield return (start: date, end: date.Add(diff));
            }
        }

我的问题是: 我如何完成它,以便它“仅”从WeeksForward中找出是应该提前一周还是提前几天。我只是想让它从Weeksforward中找出来,而不是使用相同的方法来做几乎相同的事情。

0 个答案:

没有答案