locked
Spliting a date range within a datatable RRS feed

  • Question

  • i have a datatable which contains records that contain DateFrom and DateTo.

    i need to take each record and split the date range of each record into 5 weeks slots, create a new record for each 5 week segment in a cloned datatable with a newID. The issue i am having is spliting the date range within each record down to 5 week segments and then inserting these into the new table

              DataTable dtAllocation = new DataTable();
                DataTable dtCloned = new DataTable();
    
                dtAllocation.TableName = "Allocations";
    
    
                dtAllocation = DataAccess.GetAllocationDetailsByOptionCode(optionCode);
    
                dtCloned = dtAllocation.Clone();
                dtCloned.Columns.Add(new DataColumn("myID", typeof(System.String)));
    
                foreach (DataRow drAllocation in dtAllocation.Rows)
                {
                    //i need another loop of sorts that will take
                    // drAllocation["DateFrom] and drAllocation["DateTo] 
                    // and split in 5 week ranges
    
                    DataRow dr = dtCloned.NewRow();
                    dr["DateFrom"] = //5 week date range start;
                    dr["DateTo"] = //5 week date range end
                    dr["myID"] = //Generated ID
                    dtCloned.Rows.Add(dr);
    
                    //end of date range split loop
                }
    Wednesday, April 22, 2009 10:47 AM

Answers

  • If the part of this you are having trouble with is calculating the 5 week intervals, then something like this might work:

                DateTime dt1 = DateTime.Now;
                DateTime dt2 = DateTime.Now.AddMonths(6);
    
                TimeSpan ts = dt2 - dt1;
                int fiveWeekIntervals = ts.Days / (5 * 7);
    This does *not* take any remainder into consideration, so if the time is not evenly divisible by a 5 week interval, you will have to handle the remainder.

    Hope this helps
    www.insteptech.com
    • Proposed as answer by Guo Surfer Friday, April 24, 2009 9:12 AM
    • Marked as answer by Guo Surfer Saturday, April 25, 2009 4:34 PM
    Wednesday, April 22, 2009 10:31 PM