Reading Calendar Information from Project Files
Overview
Microsoft Project allows associating various calendars with project-level work time, tasks, and resource availability. Aspose.Tasks for .NET provides full support for reading this calendar data from MPP files, including all major versions (2003–2013 and later).
This article explains how to retrieve calendar collections, read detailed calendar configuration (e.g., work weeks), and compute actual working hours between specified dates using the Aspose.Tasks API.
Retrieving Calendar Information
The
Project
class exposes the
Calendars
property, which returns a
CalendarCollection
. This collection holds all calendars defined in the project file.
To verify calendars in Microsoft Project:
- Open the project file.
- Navigate to Project > Change Working Time.
The following code demonstrates how to iterate over project calendars and retrieve their metadata.
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 Week Configurations
Calendars in Microsoft Project can define custom work weeks to override default working times. Aspose.Tasks exposes these through the
WorkWeek
class.
Work weeks can be accessed via the WorkWeeks
property on the Calendar
object. Each WorkWeek
contains metadata about the date range and working times for each day.
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}
{{}}
Calculating Working Hours Between Dates
To evaluate scheduling feasibility or resource availability, developers may need to compute the total number of working hours between two DateTime
values. Aspose.Tasks enables this through two method overloads on the Calendar
class:
Note: When specifying date ranges, provide full timestamps (hour, minute, second). If the
end
date is passed without time (e.g.,2025-08-18
), it is treated as00:00:00
and excluded from the interval.
The following example calculates working durations 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);
{{}}