Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime interval restriction in C#

Tags:

c#

datetime

The problem: I am in process of implementing a scheduler for my advisor in school. The scheduler supposes to setup a 15 minutes interval time slot from 8:00 AM to 5:00 PM, Monday to Friday. In addition, the advisor will have to specify the start and end dates of the scheduler. The scheduler will also feature an option to specify if the 15 minutes time slot is not open. Meaning my advisor will be able to mark specific time slot as NOT AVAILABLE.

What I have so far: I have 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;
        } 
    }

The class basically represents an object for one time slot in the scheduler. I also have a list of time slots that keeps a list of the valid time slots:

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

Note that a valid time slot means the following:

  1. Date is within: Monday to Friday.
  2. Time is within: 8:00 AM to 5:00 PM
  3. Time slots are within: 15 minutes interval.

In addition, I have a method that fill in the TSList as the following:

 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);
        }

    }

Notes that I was only able to satisfy the first condition in my valid time slot conditions. Thus, I was only able to restrict the dates to be within Monday to Friday range.

The questions: I am trying to achieve the missing two valid conditions for a time slot:

  1. How to restrict the times to be only 8:00am to 5:00 pm?
  2. How to make time slots separated by 15 minutes interval?
like image 300
Eyad Avatar asked Oct 24 '10 23:10

Eyad


2 Answers

First, please use DayOfWeek.Saturday and DayOfWeek.Sunday for the comparision, converting to a string is not necessary...

Then just use a simple loop like

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

This gives you startSlot values starting at 8:00am at every date ranging to 5pm (i.e. the last one is 4:45pm).

like image 66
MartinStettner Avatar answered Sep 29 '22 01:09

MartinStettner


Why are you considering building this out of nothing?

Why are you not starting with one of the many calendar management programs that are available off the shelf? For example, Microsoft Outlook contains calendar and schedule management, and you can do all of what you describe, easily. It also integrates with other scheduling tools via .ICS files, it syncs with mobile devices, syncs with Google Calendar, and so on.

But there are lots of other options. Google Calendar is another obvious one.

I don't know why you would ever consider starting from scratch. Unless it's an academic exercise (and no, I don't mean that you work in academia), then you should use larger building blocks to start.

It's like building a structure, starting with sand and water, instead of pre-fabricated concrete block.

like image 40
Cheeso Avatar answered Sep 28 '22 23:09

Cheeso