How to find the dates between two dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khaing
    New Member
    • Apr 2010
    • 5

    #1

    How to find the dates between two dates

    How can I get the dates between two dates depending on days?
    For example, if I give the start date '10-05-2010' and end date '24-05-2010' and days are Mon, Wed, Fri , the result should be '10-05-2010', '12-05-2010', '14-05-2010', '17-05-2010', '19-05-2010', '21-05-2010', '24-05-2010'.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    This looks very much like a school homework problem.

    Have you made any effort to code this beforenasking for help?

    Comment

    • khaing
      New Member
      • Apr 2010
      • 5

      #3
      It is not a school homework.
      I try to write and I did it.
      Here is the code.
      The startDate and endDate comes in dd-MM-yyyy format and the 'Days' variable contains the days (eg.,'Monday Wednesday Friday').


      Code:
      public ArrayList BetweenDates(string startDate, string endDate)
              {
                  DateTime sdt = DateTime.ParseExact(startDate, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
                  DateTime edt = DateTime.ParseExact(endDate, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
                  ArrayList dates = new ArrayList();           
                  do
                  {
      
                      if (Days.Contains(sdt.DayOfWeek.ToString()))
                      {
                          dates.Add(sdt.Date.ToString("yyyy-MM-dd"));                    
                      }
                      sdt = sdt.AddDays(1);
                  } while (sdt.CompareTo(edt) != 1);
                  return dates;
              }
      Last edited by Niheel; May 10 '10, 11:22 AM. Reason: code tags

      Comment

      Working...