I have a data structure like this which holds start/end dates:----|-----------|-----------Item| start     | end----|-----------|-----------1   |2017-05-12 | 2017-05-122   |2017-05-12 | 2017-05-133   |2017-05-13 | 2017-05-134   |2017-05-13 | 2017-05-145   |2017-05-14 | 2017-05-156   |2017-05-15 | 2017-05-167   |2017-05-16 | 2017-05-16 I want to move the start date of the first item to 2017-01-01 and every item should follow that start date and must stay within its ranges. The result should look like the following:----|-----------|-----------Item| start     | end----|-----------|-----------1   |2017-01-01 | 2017-01-012   |2017-01-01 | 2017-01-023   |2017-01-02 | 2017-01-024   |2017-01-02 | 2017-01-035   |2017-01-03 | 2017-01-046   |2017-01-04 | 2017-01-057   |2017-01-05 | 2017-01-05This is how I implemented the method:// ActiveItems() method (although it's bad naming) retrieves all the currently active hotels/transfers etc from a booking.  var items = booking.ActiveItems(); // each item has check-in and/or check-out dates, internals doesn't matter for this problemvar tempCurrentDate = newStartDate;var previousCheckoutDate = newStartDate;// go thru each itemfor (var i = 0; i < items.Count; i++){    var currentItem = items[i];    // again, bad naming but internals doesn't really matter. method returns a check-in date (DateTime)    var itemCheckinDate = currentItem.CheckInDate();     var itemCheckoutDate = currentItem.CheckOutDate();     // no of days between check-out and check-in    // this difference later added to the new check-in date to get the new check-out    var checkinToCheckoutDifference = (itemCheckoutDate - itemCheckinDate).Days;     if (i == 0)    {        // check-in equals to start date if the item is the first one in the list        itemCheckinDate = tempCurrentDate;    }    else    {        var prevDayToCurrentCheckinDifference = (previousCheckoutDate - currentItem.CheckInDate()).Days;         itemCheckinDate = tempCurrentDate.AddDays(prevDayToCurrentCheckinDifference);    }    //SetCheckInDate() internals really doesn't matter here.    currentItem.SetCheckInDate(itemCheckinDate);    previousCheckoutDate = itemCheckoutDate;    tempCurrentDate = itemCheckinDate.AddDays(checkinToCheckoutDifference);    currentItem.SetCheckOutDate(tempCurrentDate); }Even though this code works as expected, is there a better and efficient way of doing this?
Adjusting dates in an array based on a start date
c#;algorithm;datetime
Some quick remarks:Be consistent in naming. You mix both CheckOut and Checkout. Ditto for CheckIn vs Checkin.Why are CheckInDate() and CheckOutDate() methods? They should be properties. (Or you should rename each so they start with Get -- but really, they should be properties.)tempCurrentDate is not a good name for what it represents.Why did you prefix itemCheckinDate and itemCheckoutDate with item? Why not simply use checkinDate and checkoutDate?Comments should tell me why, not what. // no of days between check-out and check-in doesn't tell me anything the code doesn't.Don't needlessly abbreviate: prevDay in prevDayToCurrentCheckinDifference is unclear.Your flow is very hard to follow and seems needlessly complicated. I'd for instance leave SetCheckInDate and SetCheckOutDate to the very end instead of splitting them up. Make your flow easier to follow:store existing dates in local variablesupdate local variablesupdate bookingOr even simpler:        var earliestCheckInDate = items.Min(x => x.CheckInDate());        var offSet = (newStartDate - earliestCheckInDate).Days;        foreach (var item in items)        {            var incorrectCheckInDate = item.CheckInDate();            var incorrectCheckOutDate = item.CheckOutDate();            item.SetCheckInDate(incorrectCheckInDate.AddDays(offSet));            item.SetCheckOutDate(incorrectCheckOutDate.AddDays(offSet));        }The logic is simple: since the shift is always the same amount of days, I calculate it once (using the earliest available date) and then apply that offset on each day. You could perhaps even avoid using incorrectCheckInDate and incorrectCheckOutDate.