Managing Task Durations
Aspose.Tasks for .NET allows developers to read, set, and update task durations programmatically.
The Tsk class provides the following fields:
Duration Type:
TimeSpan
Purpose: Gets or sets the planned duration of a task.DurationFormat Type:
TimeUnitTypePurpose: Specifies the unit of duration (minutes, hours, days, weeks, etc.).
Checking Durations in Microsoft Project
In Microsoft Project, you can check a task’s duration as follows:
- Go to View > More Views > Task Entry.
- Select a task to see its Duration column.
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
- Durations drive project timelines — realistic durations lead to accurate project schedules.
- The duration format ensures tasks are consistently represented across different file types (MPP/XML).
- By programmatically controlling durations, developers can automate scheduling and maintain data integrity when exchanging project data.
FAQ
Q: What is the difference between Duration and Work in Microsoft Project?
- Duration is the total time a task takes to complete, while Work is the total effort required by resources.
Q: Can I set different duration formats (e.g., days vs. hours)?
- Yes. Use the
DurationFormatproperty with theTimeUnitTypeenumeration.
Q: Does Aspose.Tasks support duration updates in both MPP and XML files?
- Yes. Duration handling is consistent across supported Microsoft Project formats.
Q: Can I programmatically shorten project timelines by reducing durations?
- Yes. Updating task durations directly affects the project’s critical path and end date.