Working With Task Durations

Tasks take time, they have a duration. Realistic task durations help give a realistic project end date. Aspose.Tasks for C++ API allows developers to set task durations in projects.

Working with Durations

The Duration and DurationFormat properties exposed by the Tsk class are used to determine the planned duration and format of the duration of a task:

Duration in Microsoft Project

To see a task’s duration in Microsoft Project one can select More Views and then Task Entry from the View menu.

Setting task duration using Aspose.Tasks

The code example given below demonstrates how to increase and decrease the task duration to 1 week and half week respectively.

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

Calculating Durations

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

The Tsk class 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 code example demonstrates how to use this method to retrieve a task’s duration in different units: minute, day, hour, week and month.

 1// Create project instance
 2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
 3System::SharedPtr<Project> project1 = System::MakeObject<Project>(dataDir + u"TaskDurations.mpp");
 4    
 5// Get a task to calculate its duration in different formats
 6System::SharedPtr<Task> task = project1->get_RootTask()->get_Children()->GetById(1);
 7    
 8// Get the duration in Minutes, Days, Hours, Weeks and Months
 9double mins = task->Get<Duration>(Tsk::Duration()).Convert(Aspose::Tasks::TimeUnitType::Minute).ToDouble();
10System::Console::WriteLine(u"Duration in Mins: {0}", System::ObjectExt::Box<double>(mins));
11double days = task->Get<Duration>(Tsk::Duration()).Convert(Aspose::Tasks::TimeUnitType::Day).ToDouble();
12System::Console::WriteLine(u"Duration in Days: {0}", System::ObjectExt::Box<double>(days));
13double hours = task->Get<Duration>(Tsk::Duration()).Convert(Aspose::Tasks::TimeUnitType::Hour).ToDouble();
14System::Console::WriteLine(u"Duration in Hours: {0}", System::ObjectExt::Box<double>(hours));
15double weeks = task->Get<Duration>(Tsk::Duration()).Convert(Aspose::Tasks::TimeUnitType::Week).ToDouble();
16System::Console::WriteLine(u"Duration in Weeks: {0}", System::ObjectExt::Box<double>(weeks));
17double months = task->Get<Duration>(Tsk::Duration()).Convert(Aspose::Tasks::TimeUnitType::Month).ToDouble();
18System::Console::WriteLine(u"Duration in Months: {0}", System::ObjectExt::Box<double>(months));
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.