Logic to update subsequent start and end date times

Viewed 30

Suppose I have a two objects with a [start DateTime] and an [end DateTime] If I change the [start DateTime] and/or [end DateTime] of the 1st object, I want to like to apply that change to the next object's [start DateTime] and/or [end DateTime].

What would be the best way to do this without "The dates overlapping"?

NOTE: THE NEXT OBJECT SHOULD HAVE [start DateTime] and [end DateTime] AFTER THE FIRST OBJECT'S [start DateTime] and [end DateTime].

UPDATE THE time difference between each objects start and end time shouldn't change at all.

Example : Same date for all objects in this example. Can be different. OBJECT 1 ->>> START (07:58) END (08:28) OBJECT 2 ->>> START (08:48) END (10:30)

When OBJECT 1 is changed to ->> START (07:56) END (10:46) OBJECT 2 should change ->> START (after OBJECT 1 END) END (after OBJECT 2 START)

The differnce should be similar as well. For example, we added 2 hrs and 18 minutes to OBJECT 1 END, so OBJECT 2 should ideally have times increased by 2 hrs 18 minutes. Also, we decreased the OBJECT 1 START by 2 minutes. This should ideally be reflected in OBJECT 2 times as wll.

Current logic : PSEUDO CODE What I'm currently doing is,

var OBJECT1StartDifference = OBJECT1.OriginalStart - OBJECT1.Start;
var OBJECT1EndDifference = OBJECT1.OriginalEnd - OBJECT1.End;

ForEach(nextObject in ParentObject.where(obj => obj.Order > OBJECT1.order))
{
   if(OBJECT1StartDifference.HasValue)
   {
      nextObject.Start.Add(OBJECT1StartDifference);
   }

   if(OBJECT1EndDifference.HasValue)
   {
      nextObject.End.Add(OBJECT1EndDifference);
   }
}
1 Answers

I thought of something that would work.

Instead of calculating the difference between start times and end times, I'm now only calculating the difference between end times for the OBJECT1.

Then I'm applying this difference to OBJECT2 start and end time.

This will keep the OBJECT2 times always after OBJECT1 times and keep the difference between OBJECT2 start and end time the same.

Note: I also have a logic to change the OBJECT1 start time if end time is edited, and end time if start time is edited, to keep the difference between start and end time the same always.

PSEUDO CODE:

var deltaObject1EndTime = OBJECT1.EndTime - OBJECT1.PreviousEndTime; 

if(deltaObject1EndTime != o)
{
    foreach(var OBJECT in PARENTOBJECT.Objects.Where(obj => obj.Order > OBJECT1.Order))
    {
        OBJECT.StartTime.Add(deltaObject1EndTime);
        OBJECT.EndTime.Add(deltaObject1EndTime);
    }
}
Related