Get DateTime of the next nth day of the month

后端 未结 4 1393
慢半拍i
慢半拍i 2021-01-20 03:22

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?

For example, Today is the 17th of June. I wou

4条回答
  •  情话喂你
    2021-01-20 03:27

    Why not just do?

    private DateTime GetNextDate(DateTime dt, int DesiredDay)
    {
        if (DesiredDay >= 1 && DesiredDay <= 31)
        {
            do
            {
                dt = dt.AddDays(1);
            } while (dt.Day != DesiredDay);
            return dt.Date;
        }
        else
        {
            throw new ArgumentOutOfRangeException();
        }     
    }
    

提交回复
热议问题