获取该月的下一个n天的DateTime [英] Get DateTime of the next nth day of the month

查看:64
本文介绍了获取该月的下一个n天的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果给定一个日期和一个变量n,我该如何计算该月的第几天为DateTime?

If given a date and a variable n, how can I calculate the DateTime for which the day of the month will be the nth Date?

例如,今天是6月17日。
我想要一个提供15时将返回7月15日的DateTime的函数。

For example, Today is the 17th of June. I would like a function that when provided 15 would return the DateTime for July 15.

更多示例:


  • 今天是2月26日:如果提供30,函数将在3月30日返回。

  • 今天是12月28日。函数将在1月4日返回当提供时4。

  • 今天是2月28日。函数将在提供29时返回3月29日,除非是was年,在这种情况下它将返回2月29日。

推荐答案

经过许多次修改,更正和重写,这是我的最终答案:

After many, many edits, corrections and re-writes, here is my final answer:

后面的方法将返回aa DateTime ,代表下一次编号为 day 出现在日历中。它使用迭代方法执行此操作,并以扩展方法的形式针对 DateTime 对象编写,因此不限于今天的日期,但可以与任何日期一起使用。

The method that follows returns a a DateTime representing the next time the day of number day comes up in the calendar. It does so using an iterative approach, and is written in the form of an extension method for DateTime objects, and thus isn't bound to today's date but will work with any date.

代码执行以下步骤以获得所需的结果:

The code executes the following steps to get the desired result:


  1. 确保

  2. 进入一个while循环,直到永远消失(直到我们中断)。

  3. 检查 cDate 的月份是否有效(该日期必须没有过去,月份必须有足够的天数)。


    • 如果是,则返回。

    • 如果不是,则将月份增加一,将日期设置为一,然后设置 includeToday 设置为true,以便包括新月的第一天,然后再次执行循环。

  1. Ensure that the day number provided is valid (greater than zero and smaller than 32).
  2. Enter into a while loop that keeps going forever (until we break).
  3. Check if cDate's month works (the day must not have passed, and the month must have enough days in it).
    • If so, return.
    • If not, increase the month by one, set the day to one, set includeToday to true so that the first day of the new month is included, and execute the loop again.

代码:

static DateTime GetNextDate3(this DateTime cDate, int day, bool includeToday = false)
{
    // Make sure provided day is valid
    if (day > 0 && day <= 31)
    {
        while (true)
        {
            // See if day has passed in current month or is not contained in it at all
            if ((includeToday && day > cDate.Day || (includeToday && day >= cDate.Day)) && day <= DateTime.DaysInMonth(cDate.Year, cDate.Month))
            {
                // If so, break and return
                break;
            }

            // Advance month by one and set day to one
            // FIXED BUG HERE (note the order of the two calls)
            cDate = cDate.AddDays(1 - cDate.Day).AddMonths(1);

            // Set includeToday to true so that the first of every month is taken into account
            includeToday = true;
        }
        // Return if the cDate's month contains day and it hasn't passed
        return new DateTime(cDate.Year, cDate.Month, day);
    }

    // Day provided wasn't a valid one
    throw new ArgumentOutOfRangeException("day", "Day isn't valid");
}

这篇关于获取该月的下一个n天的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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