Click here to Skip to main content
15,911,315 members
Please Sign up or sign in to vote.
2.67/5 (2 votes)
See more:
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...

C#
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) //firstdaycheck
            {
                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;
        }
Posted
Updated 3-Mar-14 7:32am
v2
Comments
Sergey Alexandrovich Kryukov 3-Mar-14 13:15pm    
What are "splitted days"? :-)
—SA
aanbidmij 3-Mar-14 13:24pm    
I don't know if I explained it well...
now I have one appointment with start and End date. (start and end date are over mutiple days)
Krunal Rohit 3-Mar-14 13:19pm    
What do you want as a result ?

-KR
aanbidmij 3-Mar-14 13:27pm    
A list of "New" appointments starting with

obj 1 start of appointment => to end of day (23:59:59)
obj 2 start + 1 day (starting with 00:00:00) ending with (23:59:59)
...
last object start last day (starting with 00:00:00) ending with end time of original object
Matt T Heffron 3-Mar-14 13:42pm    
Your solution doesn't create multiple Appointments, it is just reusing the same one over again with modified StartDate/EndDate.
See my Solution below.

1 solution

Pretty close to what you have...
C#
class Appointment
{
  public DateTime StartDate { get; set; }
  public DateTime EndDate { get; set; }
}
static void Main(string[] args)
{
  var fullAppt = new Appointment() { StartDate = new DateTime(2014, 3, 3, 17, 0, 0), EndDate = new DateTime(2014, 3, 8, 8, 0, 0) };  //EDIT: corrected the example Start/End values
  List<Appointment> splitAppt = SplitAppointment(fullAppt);
}
static List<Appointment> SplitAppointment(Appointment appt)
{
  TimeSpan oneDay = TimeSpan.FromDays(1);
  TimeSpan endOfDay = oneDay - TimeSpan.FromTicks(1); // last possible time of a day
  List<Appointment> result = new List<Appointment>();
  DateTime currentDay = appt.StartDate;
  DateTime endOfCurrentDay = currentDay.Date + endOfDay;
  while (endOfCurrentDay < appt.EndDate)
  {
    result.Add(new Appointment() { StartDate = currentDay, EndDate = endOfCurrentDay });
    // copy other attributes of appt
    currentDay = currentDay.Date + oneDay;   // EDIT: changed this line
    endOfCurrentDay += oneDay;
  }
  if (currentDay < appt.EndDate)
  {
    result.Add(new Appointment() { StartDate = currentDay, EndDate = appt.EndDate });
    // copy other attributes of appt
  }

  return result;
}

Edited: See above.
 
Share this answer
 
v2
Comments
Matt T Heffron 3-Mar-14 14:42pm    
OK, I didn't test this... let me try again...See edited solution
aanbidmij 3-Mar-14 15:07pm    
Perfect! but i'm just stuggling to add all the attributes ... (YES, I am a noob) :)
Maciej Los 3-Mar-14 15:41pm    
Good job, +5!
Matt T Heffron 3-Mar-14 16:16pm    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900