【问题标题】:get the dates of days matching b/w the start date and end date every nth week每隔 n 周获取开始日期和结束日期之间匹配的日期
【发布时间】:2017-12-26 12:41:50
【问题描述】:

假设两个日期

20/06/2017 和 20/10/2017

我有一个要求,我需要在第 n 周获取开始日期和结束日期之间匹配的日期!!!!

例如:-

我从 UI 得到一些输入,上面提到的开始日期和结束日期之间的每周一和每周三每周四

我需要从 20 月 7 日到 20 月 10 日每第三周获取每周一和周四的日期

我该怎么做?

我已经写到这里了

private static List<DateTime> Get(DateTime startingDate,DateTime endingDate,int pattern)
{
    for (DateTime date = startingDate; date <= endingDate; date = date.AddDays(pattern))
        allDates.Add(date);

}

它需要一个开始和结束日期,并为我提供与模式匹配的日期。例如:- 如果 start = 20/07 且 end 为 30/07 且模式为 2,则返回 22/07,24/07,26/07....

【问题讨论】:

  • 不想变得迟钝,但是“b/w”是什么意思?
  • @STLDeveloper,很抱歉介于

标签: c# .net date datetime


【解决方案1】:

这里是您问题的简短解决方案

DateTime startDate = new DateTime(2017, 05, 1);
DateTime endDate = new DateTime(2017, 07, 21); 
Dictionary<int, string> DictionOfDates = new Dictionary<int, string>();
int weekNoCount = 3; //Every 3rd week
TimeSpan tsdiff = endDate - startDate;
int days = tsdiff.Days; //total #days in the difference from start to end
for (var i = 0; i <= days; i++)
{
    var date = startDate.AddDays(i);
    switch (date.DayOfWeek)
    {                
      case DayOfWeek.Monday:
           DictionOfDates.Add(i, date.ToShortDateString());
           break;
    }
}

foreach (var item in DictionOfDates)
{
   if (item.Key % weekNoCount == 0) //Check remainder is zero when divided by weekCount
       MessageBox.Show(item.Value); //prints the date which you want after the n'th week check
}

我想这里的一切都是不言自明的,但如果你有任何困惑,请发表评论。

编辑 1:只是解释我使用的概念:

  1. 计算两个给定日期之间的所有天数 > 这是我们的最终计数器

  2. 现在将一天增加到startDate,同时检查日期是否在给定的一天,比如说星期一(我用@ 987654325@ 案例)。如果是,则将date 添加到字典中,并将loop #no 作为索引 以供以后使用。循环直到我们碰到counter

  3. 现在对于我们在Dictionary 中的所有条目,如果 index 可以被给定的第 n 个周检查数整除,则获取日期。

就是这样!

编辑 2:围绕它创建一个方法,像这样

private void Form1_Load(object sender, EventArgs e)
{
     DateTime startDate = new DateTime(2017, 05, 1);
     DateTime endDate = new DateTime(2017, 07, 21);
     int weekNoCount = 3; //Every 3rd week
     DayOfWeek[] days = new DayOfWeek[2] { DayOfWeek.Monday, DayOfWeek.Thursday }; //Pass required days here
     FetchTheDays(startDate, endDate, weekNoCount, days);
}

private void FetchTheDays(DateTime startDate, DateTime endDate, int weekNoCount, DayOfWeek[] daysofWeek)
{
     Dictionary<int, DateTime> DictionOfDates = new Dictionary<int, DateTime>();

     TimeSpan tsdiff = endDate - startDate;
     int days = tsdiff.Days; //total #days in the difference from start to end
     for (var i = 0; i <= days; i++)
     {
         var date = startDate.AddDays(i);
         foreach (var weekday in daysofWeek)
         {
             if (date.DayOfWeek == weekday)
             DictionOfDates.Add(i, date);
         }
     }

     string testExample = "";
     foreach (var item in DictionOfDates)
     {
         if (item.Key % weekNoCount == 0) //Check remainder is zero when divided by weekCount
         testExample += (item.Value.ToShortDateString() + " (" + item.Value.DayOfWeek + ")" + "\n"); 
     }

     MessageBox.Show(testExample);
}

输出:

希望你现在已经明白了

【讨论】:

    【解决方案2】:

    这可能是另一种方法。

    using System;
    using System.Collections.Generic;
    
    
    namespace ConsoleApp3
    {
    class Program
    {
    
    
        static void Main(string[] args)
        {
            var validDates = new List<DateTime>();
            getDates(new DateTime(17,7,20), new DateTime(17,10,20),new List<DayOfWeek> { DayOfWeek.Monday, DayOfWeek.Thursday },out validDates, 3);
    
            validDates.ForEach(date => Console.WriteLine(date.ToString("dd/MM")));
            Console.ReadKey();
        }
    
        static void getDates(DateTime startDate, DateTime endDate, List<DayOfWeek> daysOfTheWeek, out List<DateTime> validDates, int pattern)
        {
            validDates = new List<DateTime>();
            for (var i = startDate; i <= endDate; i=i.AddDays(7*(pattern-1)))
            {
                for (int j = 0; j < 7; j++)
                {
                    if (daysOfTheWeek.Contains(i.DayOfWeek))
                    {
                        validDates.Add(i);
                    }
                    i=i.AddDays(1);
    
                    if (i >= endDate || i.DayOfWeek == DayOfWeek.Monday) //assuming you were counting weeks starting on Monday
                        break;
                }
            }
    
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多