Pages

Tuesday 11 March 2014

Finding all the Dates between two dates in C#

/*Pass the start date and end date for the function and get all dates comes between these two dates */
private List<DateTime> GetDateRange(DateTime StartingDate, DateTime EndingDate)                {                                                                                                                                                                if (StartingDate > EndingDate)                                                                                                                  {                                                                                                                                                           return null;                                                                                                                                                  }
List<DateTime> rv = new List<DateTime>();
DateTime tmpDate = StartingDate;
do                                                                                                                                                               {                                                                                                                                                                      /*Store all dates in List*/                                                                                                                           rv.Add(tmpDate);                  
   /*Getting each day by adding 1 to current day*/
         tmpDate = tmpDate.AddDays(1);                                                                                                    }while (tmpDate <= EndingDate);                                                                                                       return rv;                                                                                                                                                           }

No comments:

Post a Comment