第一次出现一周中的某一天

时间:2015-04-02 14:22:39

标签: c#

我想使用DateTime date配置一个事件。

我希望每个月在同一周和一周中重复这一事件。

例如,此活动将于2015年4月2日(4月的第一周的星期四)举行。

我希望每个月的第一周每个星期都重复一次。

我每次如何计算下一个日期?

3 个答案:

答案 0 :(得分:1)

从本月的第一天开始,递增,直到你得到你想要的那一周的那一天,然后增加7倍n-1,其中n是你想要的月份的第n天。

public static DateTime(int year, int month, DayOfWeek weekDay, int nth)
{
    DateTime result = new DateTime(year, month, 1);
    while(result.DatOfWeek != weekDay)
        result = result.AddDays(1);
    return result.AddDays(7 * (nth-1));
}

请注意,您希望添加对参数的检查,并确保最终结果不会转到下个月。

如果您还需要从DayOfWeek确定nthDateTime的值,那么您可以执行以下操作。

DateTime now = DateTime.Now;
DayOfWeek dayOfWeek = now.DayOfWeek;
int nth = 0;
int month = now.Month;
int year = now.Year;
while(now.Year == year && now.Month == month)
{
    now = now.AddDays(-7);
    nth++;
}

请注意,一个月可能会有第5个星期三,但下一个星期三只有4个,所以您必须确定在这些情况下该怎么做。

答案 1 :(得分:0)

我开发了一个小功能,可以找到一个月的第n天。

private static DateTime Next(int year, int month, DayOfWeek dayOfWeek, int occurrence)
        {
            return Enumerable.Range(1, 7).
                        Select(day => new DateTime(year, month, day)).
                        First(dateTime => (dateTime.DayOfWeek == dayOfWeek))
                        .AddDays(7 * (occurrence - 1));
        }

答案 2 :(得分:0)

我为项目开发的方法。它已经添加了所有边缘盒,并且现在可以投入生产。

        /// <summary>
        /// Calculates and returns the date from specifed month based on weekday and week number passed in.
        /// </summary>
        /// <param name="month">any date of the month as base referece</param>
        /// <param name="dayOfWeek">day of week Mon/Tues/Wed/Thu/Fri etc..</param>
        /// <param name="requiredWeek">!st wqeek = 1,2nd week =2,3,4,5,last etc</param>
        /// <returns></returns>
        public static DateTime GetFirstOccuringWeekDayFromMonth(DateTime month, DayOfWeek dayOfWeek, ReccurrenceEnum requiredWeek)
        {
            if (requiredWeek == ReccurrenceEnum.First || requiredWeek == ReccurrenceEnum.Second
                || requiredWeek == ReccurrenceEnum.Third || requiredWeek == ReccurrenceEnum.Forth || requiredWeek == ReccurrenceEnum.Last)
            {
                DateTime current;
                int requiredweekno = (int)requiredWeek;
                int foundDayInstanceCount = 0;
                if (requiredWeek == ReccurrenceEnum.Last)
                {
                    DateTime lastDayofMonth = new DateTime(month.AddMonths(1).Year, month.AddMonths(1).Month, 1).AddDays(-1);
                    DateTime firstDayofMonth = new DateTime(month.Year, month.Month, 1);
                    current = lastDayofMonth;
                    //skip until required day is reached.
                    while (current.DayOfWeek != dayOfWeek && current >= firstDayofMonth)
                        current = current.AddDays(-1);
                    return current;
                }
                else
                {
                    DateTime firstDayofMonth = new DateTime(month.Year, month.Month, 1);
                    DateTime lastDayofMonth = new DateTime(month.AddMonths(1).Year, month.AddMonths(1).Month, 1).AddDays(-1);
                    current = firstDayofMonth;
                    if (current.DayOfWeek == dayOfWeek)
                        foundDayInstanceCount++;
                    //skip until required week is reached.
                    while (foundDayInstanceCount < requiredweekno && current <= lastDayofMonth)
                    {
                        current = current.AddDays(1);
                        if (current.DayOfWeek == dayOfWeek)
                            foundDayInstanceCount++;
                    }
                    //skip until required day is reached.
                    while (current.DayOfWeek != dayOfWeek && current <= lastDayofMonth)
                        current = current.AddDays(1);
                    return current;
                }
            }
            else
                throw new Exception("Exception, wrong week recurrence specified!");
        }

public enum ReccurrenceEnum
    {
        First = 1,
        Second = 2,
        Third = 3,
        Forth = 4,
        Last = 5,
    }