在两个日期之间每周获取星期一的日期

时间:2019-08-25 07:19:35

标签: c# datetime timestamp

如何在给定的开始日期和结束日期之间的每个星期之后获取星期一日期

例如

  • 开始日期:2018年12月28日
  • 结束日期:2019年12月29日

因此以下日期应在列表中

Monday     : 3-12-2018
Monday     : 14-01-2019
Monday     : 28-01-2019
Monday     : 11-02-2019

这是我的代码:我无法从范围中找到每周之间的时间。如何确定从1周开始的日期。

任何帮助都将被

 DateTime startDate = LimitVmodel.StartDate;
 DateTime endDate = LimitVmodel.EndDate;

 TimeSpan diff = endDate - startDate;

 int days = diff.Days;

 for (long i = 0; i <= days; i++)
     // for (DateTime date = TIR.NetCore.PersianDateTime.ConvertShToM(LimitVmodel.StartDate); date <= TIR.NetCore.PersianDateTime.ConvertShToM(LimitVmodel.EndDate); date = date.AddDays((Double)Week))
 {
     DateTime date;
     date = startDate.AddDays(i);
     DateYear a = new DateYear();

     switch (LimitVmodel.Day)
     {
         case "Saturday":
              if (date.DayOfWeek == DayOfWeek.Saturday)
              {
                  allDates.Add(a);
              }
              break;

         case "Sunday":
              if (date.DayOfWeek == DayOfWeek.Sunday)
              {
                  allDates.Add(a);
              }
              break;

         case "Monday":
              if (date.DayOfWeek == DayOfWeek.Monday)
              {
                  allDates.Add(a);
              }
              break;
}

3 个答案:

答案 0 :(得分:1)

DateTime date = LimitVmodel.StartDate;

switch (LimitVmodel) {
    case "Monday":
        while (date.DayOfWeek != DayOfWeek.Monday) {
            date = date.AddDays(1);
        }
    break;
    case "Tuesday":
        while (date.DayOfWeek != DayOfWeek.Tuesday) {
            date = date.AddDays(1);
        }
    break;
    //Etc...
}
AllDates.add(date);
while (DateTime.compare(date, LimitVmodel.EndDate) <= 0) {
    date = date.AddDays(7);
    AllDates.add(date);
}

答案 1 :(得分:1)

这是Extension的解决方案。 获取DateTime范围和星期几。

返回DateTime列表。

public static class DateUtils
{
    public static List<DateTime> GetWeekdayInRange(this DateTime from, DateTime to, DayOfWeek day)
    {
        const int daysInWeek = 7;
        var result = new List<DateTime>();
        var daysToAdd = ((int)day - (int)from.DayOfWeek + daysInWeek) % daysInWeek;

        do
        {
            from = from.AddDays(daysToAdd);
            result.Add(from);
            daysToAdd = daysInWeek;
        } while (from < to);

        return result;
    }
}

用法:

var from = DateTime.Today; // 25/8/2019
var to = DateTime.Today.AddDays(23); // 23/9/2019
var allMondays = from.GetWeekdayInRange(to, DayOfWeek.Monday);

输出:

    {8/26/2019 12:00:00 AM}
    {9/2/2019 12:00:00 AM}
    {9/9/2019 12:00:00 AM}
    {9/16/2019 12:00:00 AM}
    {9/23/2019 12:00:00 AM}

答案 2 :(得分:0)

您可以使用var brain = require('brain.js'); // var net = new brain.NeuralNetwork(); var net = new brain.recurrent.LSTM(); net.train([ {input: [0.9525741268224331], output: [0.9998766054240137]}, {input: [0.9998766054240137], output: [0.9996646498695336]}, {input: [0.9996646498695336], output: [1]}, {input: [0.9990889488055994], output: [1]} ]); // var input = 1/(1+Math.pow(Math.E, -6)); // var output = net.run(); var output = net.run([/* insert the input here for which you want the network to guess the value for like */0.99 ]) console.log(output);// gives output = 1 or close figure to it循环来检查进入for的日期,并整理当天的所有日期。像这样:

LimitVmodel.Day