Calendar: Business Hours

calendar-business-hours.png

The Calendar control supports business hours:

  • The range can be set using BusinessBeginsHour (defaults to 9) and BusinessEndsHour (defaults to 18) properties.
  • The colors of the business and non-business time cells are defined using BackColor and NonBusinessBackColor.

You can define custom time cell colors using BeforeCellRender event:

protected void DayPilotCalendar1_BeforeCellRender(object sender, BeforeCellRenderEventArgs e)
    {
        if (e.Start.Hour >= 9 && e.Start.Hour < 12)
            e.BackgroundColor = "#FFF2CC"; // shift #1 
        else if (e.Start.Hour >= 12 && e.Start.Hour < 15)
            e.BackgroundColor = "#FFD9CC"; // shift #2
        else if (e.Start.Hour >= 15 && e.Start.Hour < 18)
            e.BackgroundColor = "#F2FFCC"; // shift #3
    }

Turning Saturday and Sunday into business days:

    protected void DayPilotCalendar1_BeforeCellRender(object sender, BeforeCellRenderEventArgs e)
    {
        if ((e.Start.DayOfWeek == DayOfWeek.Saturday || e.Start.DayOfWeek == DayOfWeek.Sunday)
            || (e.Start.Hour >= 9 && e.Start.Hour < 18))
        {
            e.IsBusiness = true;
        }
   }

You can also change just the IsBusiness property and you will get the color automatically:

    protected void DayPilotCalendar1_BeforeCellRender(object sender, BeforeCellRenderEventArgs e)
    {
        if (e.Start.Hour >= 10 && e.Start.Hour < 12)
            e.IsBusiness = true;
        else if (e.Start.Hour >= 14 && e.Start.Hour < 16)
            e.IsBusiness = true;
        else
            e.IsBusiness = false;
    }

See Also

DayPilot for ASP.NET WebForms, DayPilot for ASP.NET MVC, DayPilot for Java