Adding and Removing Calendar Exceptions

Working with Calendar Exceptions

Aspose.Tasks helps you manage calendar exceptions. The Exceptions property exposed by the Calendar class represents a collection of CalendarException objects and is used to add, remove, or iterate through calendar exceptions.

This functionality is useful when working with non-standard working days, holidays, or recurring exception patterns in project calendars.

Example: Add and Remove Calendar Exceptions

The following example demonstrates how to:

 1Project project = new Project("New Project.mpp");
 2
 3// Remove an exception
 4Calendar cal = project.Calendars.ToList()[0];
 5if (cal.Exceptions.Count > 1)
 6{
 7    CalendarException exc = cal.Exceptions.ToList()[0];
 8    cal.Exceptions.Remove(exc);
 9}
10
11// Add an exception
12CalendarException calExc = new CalendarException();
13calExc.FromDate = new System.DateTime(2009, 1, 1);
14calExc.ToDate = new System.DateTime(2009, 1, 3);
15cal.Exceptions.Add(calExc);
16
17// Display exceptions
18foreach (CalendarException calExc1 in cal.Exceptions)
19{
20    Console.WriteLine("From" + calExc1.FromDate.ToShortDateString());
21    Console.WriteLine("To" + calExc1.ToDate.ToShortDateString());
22}

Conclusion

Managing calendar exceptions is essential for ensuring that your project schedule reflects real-world constraints, such as holidays or planned downtimes. Aspose.Tasks for .NET makes it easy to programmatically handle these exceptions for accurate scheduling and resource planning.