Read Calendar Information from Project Files

Retrieving Calendar Information

Microsoft Project lets users associate a variety of different calendars with working time, tasks and resources. These features are also available in Aspose.Tasks. This topic explains how to retrieve information about which calendars are used in a project. Aspose.Tasks can retrieve calendar information for all versions of Microsoft Project: 2003, 2007, 2010, and 2013.

The Calendars property exposed by the Project class supports List of Aspose.Tasks.Calendar objects. This property can be used to retrieve the calendar information carried by the project.

To view calendar information in Microsoft Project:

  1. Open a project.
  2. From the Project menu, select Change Working Time.

The following piece of code retrieves the calendar information from a project.

 1// Create a project instance
 2Project project = new Project("RetrieveCalendarInfo.mpp");
 3
 4// Retrieve Calendars Information
 5CalendarCollection calendars = project.Calendars;
 6foreach (Calendar cal in calendars)
 7{
 8    if (cal.Name != null)
 9    {
10        Console.WriteLine("Calendar UID : " + cal.Uid);
11        Console.WriteLine("Calendar Name : " + cal.Name);
12
13        WeekDayCollection alDays = cal.WeekDays;
14        foreach (WeekDay wd in alDays)
15        {
16            TimeSpan ts = wd.GetWorkingTime();
17            if (wd.DayWorking)
18            {
19                Console.WriteLine(wd.DayType.ToString() + ":");
20                Console.WriteLine(ts.ToString());
21            }
22        }
23    }
24}

Reading Work Weeks Information from Calendar

Aspose.Tasks for .NET API can read Work weeks information from any of the Project’s calendar. The WorkWeek class can be used to achieve this.

 1Project project = new Project("ReadWorkWeeksInformation.mpp");
 2Calendar calendar = project.Calendars.GetByUid(3);
 3WorkWeekCollection collection = calendar.WorkWeeks;
 4
 5foreach (WorkWeek workWeek in collection)
 6{
 7    DateTime fromDate = workWeek.FromDate;
 8    DateTime toDate = workWeek.ToDate;
 9
10    // This data is all about "Details." button you can set special working times for special WeekDay or even make it nonworking
11    WeekDayCollection weekDays = workWeek.WeekDays;  
12    foreach (WeekDay day in weekDays) 
13    {
14        // You can further traverse through working times and display these
15        WorkingTimeCollection workingTimes = day.WorkingTimes;
16    }
17}

Getting Working Hours On or Between Dates

Working hours are typically defined as a range of hours over a day, on specific days of the week. For example, 09:00 – 17:00, Monday to Friday. With Aspose.Tasks for .NET, it is possible to calculate the number of working hours that fall on a date, or between two dates. Finding out the number of working dates between two dates help project managers figure out whether there are enough hours to get work done, or if they need to find more resources to complete a task. It is recommended that for calculating the working hours between two dates, complete date and time (hours, minutes and seconds) should be provided for exact calculation. If the only date is given as finish date value say 2016, 8, 18, it will be considered as 0:0:0 hours on that day and, hence, finish date day will not be included in the calculation. However, if the finish date is given as 2016,8,18,17,0,0, then the proper result will be calculated.

The Calendar class exposes two methods(overloaded) for finding working hours on or between dates. GetWorkingHours(DateTime dt) and GetWorkingHours(DateTime startdt, DateTime enddt) to define the working hours between two dates.

The following example shows how to find the duration in minutes, hours and days between two dates.

 1// Load an existing project
 2Project project = new Project("New Project.mpp");
 3
 4// Access Task By Id
 5Task task = project.RootTask.Children.GetById(1);
 6
 7// Access Calendar and it's start and end dates
 8Calendar taskCalendar = task.Get(Tsk.Calendar);
 9DateTime startDate = task.Get(Tsk.Start);
10DateTime endDate = task.Get(Tsk.Finish);
11DateTime tempDate = startDate;
12
13// Access resource and their calendar
14Resource resource = project.Resources.GetByUid(1);
15Calendar resourceCalendar = resource.Get(Rsc.Calendar);
16
17TimeSpan timeSpan;
18
19// Get Duration in Minutes
20double durationInMins = 0;
21while (tempDate < endDate)
22{
23    if (taskCalendar.IsDayWorking(tempDate) && resourceCalendar.IsDayWorking(tempDate))
24    {
25        timeSpan = taskCalendar.GetWorkingHours(tempDate);
26        durationInMins = durationInMins + timeSpan.TotalMinutes;
27    }
28    tempDate = tempDate.AddDays(1);
29}
30tempDate = startDate;
31
32// Get Duration in Hours
33double durationInHours = 0;
34while (tempDate < endDate)
35{
36    if (taskCalendar.IsDayWorking(tempDate) && resourceCalendar.IsDayWorking(tempDate))
37    {
38        timeSpan = taskCalendar.GetWorkingHours(tempDate);
39        durationInHours = durationInHours + timeSpan.TotalHours;
40    }
41    tempDate = tempDate.AddDays(1);
42}
43tempDate = startDate;
44
45// Get Duration in Days
46double durationInDays = 0;
47while (tempDate < endDate)
48{
49    if (taskCalendar.IsDayWorking(tempDate) && resourceCalendar.IsDayWorking(tempDate))
50    {
51        timeSpan = taskCalendar.GetWorkingHours(tempDate);
52        if (timeSpan.TotalHours > 0)
53        {
54            durationInDays = durationInDays + timeSpan.TotalDays * (24 / (timeSpan.TotalHours));
55        }
56    }
57    tempDate = tempDate.AddDays(1);
58}
59
60Console.WriteLine("Duration in Minutes = " + durationInMins);
61Console.WriteLine("Duration in Hours = " + durationInHours);
62Console.WriteLine("Duration in Days = " + durationInDays);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.