Retrive Dates between 2 dates

How can we retrive working dates (consider a have 5 working days) between 2 dates.

we can input like  getdates(startdate,enddate,noofworkingdays) and the output become

01/01/2006,02/01/2006 ,01/03/2006...... and so on . i have to do the same in c#.

thanks

alal

 




Answer this question

Retrive Dates between 2 dates

  • siosio

    The work week is relative to who you are and what culture you live in. Instead a more useful function might be to retrieve the DateTime values between a given date range excluding those with a specific day of the week.

    public Collection<DateTime> GetFilteredDateRange ( DateTime start, DateTime end, DayOfWeek[] excludeWeekdays )
    {
    Collection<DateTime> range =
    new Collection<DateTime>();
    DateTime current = start;

    while (current <= end)
    {
    if (Array.IndexOf(excludeWeekdays, current.DayOfWeek) < 0)
    range.Add(current);

    /
    /Next day
    current = current.AddDays(1.0);
    };
    return range;
    }

    This function is concise but slow for large ranges. A better approach (if needed) would be to start at the front of the date range and simply skip over the week days in blocks until you reach a Saturday. Then skip 2 days and repeat again until you exceed the end range. This however is assuming things that might not be true for your culture.

    Michael Taylor - 5/11/06


  • Jack Hoxley

    Hopefully I've understood your requirements and this piece of code will help.

    However, could you expand upon your noofworkingdays requirement please

    private string GetWorkingDays(DateTime start, DateTime end)

    {

    string retStr = "";

    while (start != end)

    {

    if ((start.DayOfWeek != DayOfWeek.Sunday) && (start.DayOfWeek != DayOfWeek.Saturday))

    {

    retStr = retStr + start.ToShortDateString();

    if (retStr.Length > 1) retStr = retStr + ", ";

    }

    start = start.AddDays(1);

    }

    return retStr;

    }

    Use it like so:

    DateTime start = new DateTime(2006, 05, 01);

    DateTime end = new DateTime(2006, 05, 15);

    MessageBox.Show (GetWorkingDays(start, end));

    HTH



  • Retrive Dates between 2 dates