Visual C#: Detect Conflict Schedule
Posted: 18 Jan 2010, 16:55pm - Monday

[caption id="attachment_329" align="alignright" width="271" caption="Screenshot"]Screenshot[/caption] It seems a lot of people are searching about solving conflict schedule. So I decided to create a sample. Below is the core code of checking conflict schedule...

// sample conflict detection (defined) [start]
DateTime d = new DateTime(2010, 1, 13);
richTextBox1.Text = DateTime.Now.ToLongDateString() + " = " + d.ToLongDateString() + "\n";
if (DateTime.Now.CompareTo(d) > 0)
{
richTextBox1.Text += "true\n" + DateTime.Now.CompareTo(d).ToString();
}
else
{
richTextBox1.Text += "false\n" + DateTime.Now.CompareTo(d).ToString();
}
richTextBox1.Text += "\n\n";
DateTime dx = DateTime.Now;
//MessageBox.Show(dx.Hour.ToString());
DateTime[] dt = new DateTime[4];
// enrolled schedule
dt[0] = new DateTime(int.Parse(dx.Year.ToString()), int.Parse(dx.Month.ToString()), int.Parse(dx.Day.ToString()), 8, 0, 0);
dt[1] = new DateTime(int.Parse(dx.Year.ToString()), int.Parse(dx.Month.ToString()), int.Parse(dx.Day.ToString()), 9, 0, 0);
// adding new schedule
dt[2] = new DateTime(int.Parse(dx.Year.ToString()), int.Parse(dx.Month.ToString()), int.Parse(dx.Day.ToString()), 9, 0, 0);
dt[3] = new DateTime(int.Parse(dx.Year.ToString()), int.Parse(dx.Month.ToString()), int.Parse(dx.Day.ToString()), 10, 0, 0);
// checking schedule conflict
if (((dt[0].CompareTo(dt[2]) < 0) && (dt[1].CompareTo(dt[2]) > 0)) || (dt[0].ToShortTimeString() == dt[2].ToShortTimeString()))
{
richTextBox1.Text += dt[0].ToShortTimeString() + " - " + dt[1].ToShortTimeString() + " against " + dt[2].ToShortTimeString() + " - " + dt[3].ToShortTimeString() + "\nResult: CONFLICT";
}
else
{
richTextBox1.Text += dt[0].ToShortTimeString() + " - " + dt[1].ToShortTimeString() + " against " + dt[2].ToShortTimeString() + " - " + dt[3].ToShortTimeString() + "\nResult: NO CONFLICT";
}
// sample conflict detection (defined) [end]
If you want to download the whole code, link below and enjoy... Do not practice the copy and paste! :) Download: [download id="6"]