Getting all DateTimes between two 'DateTime's in C#

前端 未结 8 670
清酒与你
清酒与你 2020-12-12 20:14

I have two DateTimes, and I want to get all DateTimes between these Dates. Such as, if my Dates are like 01.01.2010 - 05.01.2010, my function shoul

8条回答
  •  一个人的身影
    2020-12-12 20:50

    Here is a quick console app to demonstrate how to do it - use AddDays() instead:

    class Program
    {
        static void Main(string[] args)
        {
    
            GetDates(new DateTime(2010, 1, 1), new DateTime(2010, 2, 5));
    
            Console.ReadKey();
        }
    
        static List GetDates(DateTime startDate, DateTime endDate)
        {
            List dates = new List();
    
            while ((startDate = startDate.AddDays(1)) < endDate)
            {
                dates.Add(startDate);
            }
    
            return dates;
        }
    }
    

提交回复
热议问题