Calculate Task Duration in Different Units

[ Hide Show ]

Calculating Durations

It can be useful to calculate the duration of a task in different units such as minutes, hours, etc.

The Tsk class offers provides the Duration property for accessing task duration, which returns the Duration class object. The Convert method exposed as part of Duration class can then be used to calculate task durations in different units. This method takes TimeUnitType as the input argument and returns the duration as a double value.

The following piece of code shows how to use this method to retrieve a task’s duration in different units: minute, day, hour, week and month.

 1Project project = new Project("New Project.mpp");
 3// Get a task to calculate its duration in different formats
 4Task task = project.RootTask.Children.GetById(1);
 6// Get the duration in Minutes, Days, Hours, Weeks and Months
 7double mins = task.Get(Tsk.Duration).Convert(TimeUnitType.Minute).ToDouble();
 8Console.WriteLine("Duration in Mins: {0}", mins);
 9double days = task.Get(Tsk.Duration).Convert(TimeUnitType.Day).ToDouble();
10Console.WriteLine("Duration in Days: {0}", days);
11double hours = task.Get(Tsk.Duration).Convert(TimeUnitType.Hour).ToDouble();
12Console.WriteLine("Duration in Hours: {0}", hours);
13double weeks = task.Get(Tsk.Duration).Convert(TimeUnitType.Week).ToDouble();
14Console.WriteLine("Duration in Weeks: {0}", weeks);
15double months = task.Get(Tsk.Duration).Convert(TimeUnitType.Month).ToDouble();
16Console.WriteLine("Duration in Months: {0}", months);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.