如何在特定日期获得下个月的第一个星期日?

时间:2015-03-13 06:45:24

标签: c# datetime

我正在寻找一些逻辑,使用c#让日期在下个月的第一个星期日达到给定日期。 如何使用c#获取给定日期的下个月的第一个星期日?

6 个答案:

答案 0 :(得分:1)

public static DateTime GetFirstSundayOfNextMonth(DateTime givenDate)
{
    DateTime firstDayNextMonth = givenDate.AddDays(-givenDate.Day + 1).AddMonths(1);
    int diff = 7 - (int)firstDayNextMonth.DayOfWeek;
    return firstDayNextMonth.AddDays(diff);
}

答案 1 :(得分:0)

您没有显示任何工作,但让我们一步一步地编写Jon says代码;

  

如何获得给定日期下个月的第一个星期日

我们在您的问题中针对指定日期dt称为DateTime。所有月份均以1作为日期编号开头。这就是为什么我们可以创建一个名为DateTime

firstDayOfNextMonth
DateTime firstDayOfNextMonth = new DateTime(dt.Year, dt.Month + 1, 1);

现在怎样?当然我们需要检查一天的名字,它是星期天还是不。到什么时候?直到我们找到一天作为星期天。这就是为什么我们需要迭代这一天(每天 1天),直到找到它为止。我们可以使用DayOfWeek enumeration作为它的日期名称。

我们在代码中将DateTime定义为firstSundayOfNextMonth;

DateTime firstSundayOfNextMonth;

现在迭代下周的第一天,直到我们找到一个星期天;

while (firstDayOfNextMonth.DayOfWeek != DayOfWeek.Sunday)
{
    firstDayOfNextMonth = firstDayOfNextMonth.AddDays(1);
}

当这个while语句停止工作时?是的,当我们找到第一个星期天。这就是为什么我们需要将此值分配给此循环之外的firstSundayOfNextMonth变量。

while (firstDayOfNextMonth.DayOfWeek != DayOfWeek.Sunday)
{
    firstDayOfNextMonth = firstDayOfNextMonth.AddDays(1);
}

firstSundayOfNextMonth = firstDayOfNextMonth;

我们找到了它。 firstSundayOfNextMonth是给定DateTime下个月的第一个星期日(在我们的情况下为dt)。

答案 2 :(得分:0)

试试这个:

 DateTime dt1 = new DateTime(2015, 03, 13);
 DateTime dm = dt1.AddMonths(1);
 DateTime addm = new DateTime(dm.Year,dm.Month,1);
 var nextSunday = addm.AddDays(7 - (int)addm.DayOfWeek);  
  

您可以将2015, 03, 13替换为year,month,day

答案 3 :(得分:0)

为了获得某个日期之后的下一个星期,我找到了一个简单的方法。

public static DateTime FirstDayOfWeekOnOrAfter(this DateTime date, DayOfWeek dayOfWeek)
{
  return date.AddDays(Math.Abs(dayOfWeek - date.DayOfWeek));
}

public static DateTime FirstDayOfWeekOnOrBefore(this DateTime date, DayOfWeek dayOfWeek)
{
  return date.AddDays(-Math.Abs(dayOfWeek - date.DayOfWeek));
}

答案 4 :(得分:0)

DateTime nextMonthfirstSunday;
        DayOfWeek firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month+1, 1).DayOfWeek;
        switch (firstDay)
        {
            case DayOfWeek.Sunday:
                nextMonthfirstSunday = new DateTime(DateTime.Now.Year, DateTime.Now.Month+1, 1);
                break;
            case DayOfWeek.Monday:
                nextMonthfirstSunday = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 7);
                break;
            case DayOfWeek.Tuesday:
                nextMonthfirstSunday = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 6);
                break;
            case DayOfWeek.Wednesday:
                nextMonthfirstSunday = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 5);
                break;
            case DayOfWeek.Thursday:
                nextMonthfirstSunday = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 4);
                break;
            case DayOfWeek.Friday:
                nextMonthfirstSunday = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 3);
                break;
            default://Saturday
                nextMonthfirstSunday = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 2);
                break;
        }

答案 5 :(得分:0)

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>repl.it</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <div class="ajaxModalBody">
    <div class="container-fluid">
      <div id="AddItemOption">
        <div class="d-flex justify-content-center align-items-center">
          <table class="table table-striped table-condensed table-additems">
            <thead>
              <tr>
                <th class="align-middle border-0">items</th>
                <th class="align-middle border-0">Delete</th>
              </tr>
            </thead>
            <tbody>
              <tr id="row1">
                <td>
                  <select name="items[]" class="form-control sellitem">
                    <option value="book">book</option>
                    <option value="pencil">pencil</option>
                    <option value="pen">pen</option>
                    <option value="other">other</option>
                  </select>

                  <input type="text" class='form-control inputother' style="display: none;" name="other[]">

                </td>
                <td class="action">
                  <button type="submit" class="btn btn-danger ">
                                    Delete
                                </button>
                </td>
              </tr>
              <tr>
                <td colspan="4">
                  <button type="button" class="btn btn-default btn-xs">+ Add New Item</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
  <script src="script.js"></script>
</body>

</html>