Reputation: 77
How I have to convert Task.Duration
that get valid result?
I find explanations about this property in MSDN:
Gets or sets the duration (in minutes) of a task.
But it doesn't work correct.
If I divided result by 60 (minutes in hour) and 24(hours in day) I get incorrect result.
But if I divided by 20 and 24 all it's ok. And I don't understand why.
I use C# on .Net 3.5 and Office Primary Interop Assemblies ( Microsoft.Office.Interop.MSProject
for office 2010).
I use that code :
void SetProperties(MSProject.Task o, string version)
{
Wbs = o.WBS.ToString();
Name = o.Name.ToString();
StartDate = (System.DateTime) o.Start;
FinishDate = (System.DateTime)o.Finish;
Iteration = version;
duration = (Convert.ToInt16(o.Duration)/10/24).ToString();//after result //divided by 2 I get correct result. Why?
}
thanks
Upvotes: 3
Views: 2434
Reputation: 10229
The reason that it doesn't work like you expect is because in a day you do not have 24 hours of working time. The Duration of a task is the amount of working time between the start and finish, not the absolute number of hours.
Since the default number of working hours in a day is 8, you divide the total minutes by 480 (60 min * 8 hours) to get the number of days. Your calculation of 20 * 24 just so happens to also equal 480, so you stumbled upon the correct number.
Of course, do not expect that Start + Duration (in days) is going to equal your Finish date. That's because you also have to factor in non-working days, like weekends. So you can have a 3 day task that starts on Friday, and it will not finish until the end of the day on Tuesday (5 calendar days).
Upvotes: 4