Managing Task Durations

Aspose.Tasks for .NET allows developers to read, set, and update task durations programmatically.

The Tsk class provides the following fields:

Checking Durations in Microsoft Project

In Microsoft Project, you can check a task’s duration as follows:

Example: Setting Task Duration Programmatically

The following example demonstrates how to set and update task durations in C#:

 1// Create a new project and add a new task
 2Project project = new Project();
 3Task task = project.RootTask.Children.Add("Task");
 4
 5// Task duration in days (default time unit)
 6Duration duration = task.Get(Tsk.Duration);
 7Console.WriteLine("Duration equals 1 day: {0}", duration.ToString().Equals("1 day"));
 8
 9// Convert to hours time unit
10duration = duration.Convert(TimeUnitType.Hour);
11Console.WriteLine("Duration equals 8 hrs: {0}", duration.ToString().Equals("8 hrs"));
12
13// Get wrapped TimeSpan instance
14Console.WriteLine("Duration TimeSpan equals to TimeSpan of 8 hrs: {0}", duration.TimeSpan.Equals(TimeSpan.FromHours(8)));
15
16// Increase task duration to 1 week and display if duration is updated successfully
17task.Set(Tsk.Duration, project.GetDuration(1, TimeUnitType.Week));
18Console.WriteLine("Duration equals 1 wk: {0}", task.Get(Tsk.Duration).ToString().Equals("1 wk"));
19
20// Decrease task duration and display if duration is updated successfully
21task.Set(Tsk.Duration, task.Get(Tsk.Duration).Subtract(0.5));
22Console.WriteLine("Duration equals 0.5 wks: {0}", task.Get(Tsk.Duration).ToString().Equals("0.5 wks"));

This example increases and decreases a task’s duration to 1 week and half a week respectively.

Key Notes

FAQ

Q: What is the difference between Duration and Work in Microsoft Project?

Q: Can I set different duration formats (e.g., days vs. hours)?

Q: Does Aspose.Tasks support duration updates in both MPP and XML files?

Q: Can I programmatically shorten project timelines by reducing durations?

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.