Hi folks,
First of all sorry for my bad English! but I have a problem that I can't solve ...
I have an object (Appointment) which has several attributes (2 of them are named StartDate and EndDate)
e.g.:
- StartDate = 2014/3/3 17:00:00
- EndDate = 2014/3/8 08:00:00
Is it possible to split these Appointments as following:
Appointment => StartDate = 2014/3/3 17:00:00 EndDate = 2014/3/3 23:59:59 + original attributes
Appointment => StartDate = 2014/3/4 00:00:00 EndDate = 2014/3/4 23:59:59 + original attributes
Appointment => StartDate = 2014/3/5 00:00:00 EndDate = 2014/3/5 23:59:59 + original attributes
Appointment => StartDate = 2014/3/6 00:00:00 EndDate = 2014/3/6 23:59:59 + original attributes
Appointment => StartDate = 2014/3/7 00:00:00 EndDate = 2014/3/7 23:59:59 + original attributes
Appointment => StartDate = 2014/3/8 00:00:00 EndDate = 2014/3/8 08:00:00 + original attributes
And return this as a List<appointments> (splitted Appointments)
It seems possible but at this moment I can't figure it out
I have googled for this problem but neither google doesn't find the answer...
Many thanks in advance!
Crappy code I have for the moment...
private List<Appointment> SplitMultidayAppointments(Appointment app)
{
DateTime startdate = app.StartDate;
DateTime enddate = app.EndDate;
List<Appointment> splitdates = new List<Appointment>();
DateTime newStartDate = new DateTime();
DateTime newEndDate = new DateTime();
Appointment split = app;
if ((enddate - startdate).TotalDays > 1)
{
split.StartDate = app.StartDate;
split.EndDate = new DateTime(split.StartDate.Year, split.StartDate.Month, split.StartDate.Day, 23, 59, 59);
splitdates.Add(split);
newStartDate = split.EndDate.AddSeconds(1);
}
while (DateTime.Compare(enddate, newStartDate) >0)
{
split.StartDate = newStartDate;
split.EndDate = new DateTime(newStartDate.Year, newStartDate.Month, newStartDate.Day, 23, 59, 59);
splitdates.Add(split);
newStartDate = newStartDate.AddDays(1);
}
split.StartDate = newStartDate;
split.EndDate = app.EndDate;
splitdates.Add(split);
return splitdates;
}