DateTime interval limitation in C #

Problem: I am in the process of introducing a planner for my school adviser. The scheduler expects to set an intermediate time interval of 15 minutes from 8:00 to 17:00 from Monday to Friday. In addition, the adviser will have to indicate the start and end dates of the scheduler. The scheduler will also be able to indicate whether a 15-minute time interval is open. So, my adviser will be able to mark a certain time interval as NOT AVAILABLE.

What I still have: I created a simple class:

public class TimeSlot  
    {
        public DateTime dateTime
        {
            get;
            set;
        }

        public bool isAvailable
        {
            get;
            set;
        }

        TimeSlot(DateTime dt, bool Avalible)
        {
            dateTime = dt;
            isAvailable = Avalible;
        } 
    }

. , :

List<TimeSlot> TSList = new List<TimeSlot>();

, :

  • : .
  • : 8:00 17:00.
  • : 15 .

, , TSList :

 private void button_Next_Click(object sender, RoutedEventArgs e)
    {
        /* Getting the values of fromDate and toDate from the GUI controls*/
        DateTime fromDate = datePicker1.SelectedDate.Value;
        DateTime toDate = datePicker2.SelectedDate.Value;

        while (fromDate <= toDate)
        {
            /*This ensures that we only deal with days Monday to Friday*/
            if (fromDate.DayOfWeek.ToString() != "Saturday" && fromDate.DayOfWeek.ToString() != "Sunday")
            {
                /*PROBLEM HERE!!*/
            }

            /*Updating fromDate: Incrementing fromDate by 1 day*/
            fromDate = fromDate.AddDays(1);
        }

    }

, . , .

: :

  • 8:00 5:00 ?
  • 15 ?
+5
4

-, DayOfWeek.Saturday DayOfWeek.Sunday , ...

,

DateTime startSlot = fromDate.Date.AddHours(8); // Starts at 8:00AM
while (startSlot.Hour < 17) {
  // Construct time slot class
  startSlot = startSlot.AddMinutes(15);
}

startSlot 8:00 5 (.. 4:45 ).

+3

?

, ? , Microsoft Outlook , , , . .ICS, , Google Calendar ..

. Google - .

, - , . ( , , ), .

, , .

+3

. , .

        // Round interval
        const int roundInterval = 15;

        var remainder = fromDate.TimeOfDay.Minutes % roundInterval;

        var curTime = remainder == 0 ? fromDate : fromDate.AddMinutes(roundInterval - remainder);
        curTime = curTime.AddSeconds(-curTime.TimeOfDay.Seconds);

        var delta = TimeSpan.FromMinutes(roundInterval);

        while (curTime < toDate)
        {
            while (curTime.DayOfWeek == DayOfWeek.Saturday || curTime.DayOfWeek == DayOfWeek.Sunday)
            {
                curTime = curTime.Date.AddDays(1);
            }

            if (curTime.TimeOfDay.Hours < 8)
            {
                curTime = curTime.AddHours(8 - curTime.TimeOfDay.Hours);
                curTime = curTime.AddMinutes(-curTime.TimeOfDay.Minutes);
                continue;
            }

            if (curTime.TimeOfDay.Hours >= 17)
            {
                curTime = curTime.AddHours(24 - curTime.TimeOfDay.Hours);
                curTime = curTime.AddMinutes(-curTime.TimeOfDay.Minutes);
                continue;
            }

            TSList.Add(new TimeSlot(curTime, true));

            curTime = curTime.Add(delta);
        }
    }
+1
DateTime myScheduledTimeSlot = new DateTime(2010, 10, 26, 8, 45, 0);

// Use existing check to check day of week constraint...

// Check if the datetime falls on a correct minute boundary
switch (myScheduledTimeSlot.Minute)
{
  case 0:
  case 15:
  case 30:
  case 45:
    // The time slot is valid
    break;
  default:
    // The time slot is not valid
    break;
}  

Just check to see if it hits the 15-minute slot, as you don’t have any weird borders that keep the same every hour. I would recommend checking out Quart.NET if you want to save time creating events / scheduling.

-1
source

All Articles