User wants to edit start date. Days between start date and end date must always be the same.
For example
Start Date is originally 28/07/2014 and End Date is 01/08/2014
The dates are 4 days apart
User changes start date to 01/06/2014
End Date must now be 05/06/2014
Having difficulty wrapping my head around this logic.
There are two aspects to this:
Working out the number of days between two dates
Personally, I'd use my Noda Time API for this, but you could still use the BCL's DateTime
type - subtract one DateTime
from the other to get a TimeSpan
, and then use TimeSpan.TotalDays
property. Then convert the result to an integer. (You'll need to make sure they really are just dates, to avoid getting a fraction of a day.) You could even just keep the TimeSpan
if you wanted to.
Adjusting one date based on another
If you know how many days you want to keep the two apart, you just need to use DateTime.AddDays
appropriately. For example, if startDate
has changed:
endDate = startDate.AddDays(days)
And if endDate
has changed:
startDate = endDate.AddDays(-days)
See more on this question at Stackoverflow