Start and Finish dates of task not getting reflected

Hi,


I am trying to set the start and finish dates of a task from a hidden variable.

Task t1 = proj.RootTask.Children.Add(“tested now”);
t1.Set(Tsk.Start, Convert.ToDateTime(hdnStartDate.Value));
t1.Set(Tsk.Finish, Convert.ToDateTime(hdnEndDate.Value));

It is not setting the date. I have set the project start date as 15Jun2016 and end date as 31dec2016. The task that i create is taking start and finish dates by default as 30dec2016 and 31dec2016. Even if I set it, it remains the same. Why???

Regards

Hi Purnima,


I have tried to re-produce the scenario but could not succeed. Could you please send us complete code which can be compiled and executed here along with the template project file? It will help us to reproduce the scenario and provide assistance accordingly.

Hi,


Following is the code:

protected void Page_Load(object sender, EventArgs e)
{
Aspose.Tasks.License lic = new Aspose.Tasks.License();
lic.SetLicense(“Aspose.Tasks.lic”);
}
protected void btnOnClick(object sender, EventArgs e)
{
Project proj = new Project();
DateTime projectstartdate = Convert.ToDateTime(“2016,01,01”);
DateTime projectenddate = Convert.ToDateTime(“2016,12,31”);
proj.Set(Prj.StartDate, projectstartdate);
proj.Set(Prj.FinishDate, projectenddate);
Task tsk = proj.RootTask.Children.Add(“Task 1”);
tsk.Set(Tsk.Start, projectstartdate);
tsk.Set(Tsk.Finish, projectenddate);
proj.Save(“E:\output.xml”, Aspose.Tasks.Saving.SaveFileFormat.XML);
}


Task 1 in the project plan does not have default start and end dates as 01/01/2016 and 31/12/2016, whereas it has 30/12/2016 and 31/12/2016

Hi Purnima,


Thank you for contacting Aspose support again.

When a task is created, it contains constraint type “AsSoonAsPossible” however when Tsk.Finish flag is used to set the finish date of the task, the constraint type is set to “FinishNoEarlierThan”. That is why dates are changed. You may please modify the code as given below to get the desired result.



Project proj = new Project();
DateTime projectstartdate = Convert.ToDateTime(“2016,01,01”);
DateTime projectenddate = Convert.ToDateTime(“2016,12,31”);
proj.Set(Prj.StartDate, projectstartdate);
proj.Set(Prj.FinishDate, projectenddate);
Task tsk = proj.RootTask.Children.Add(“Task 1”);

tsk.Set(Tsk.Start, projectstartdate);
tsk.Set(Tsk.Finish, projectenddate);

Console.WriteLine("Before setting constraint = " + tsk.Get(Tsk.ConstraintType));
Console.WriteLine("Before setting constraint date = " + tsk.Get(Tsk.ConstraintDate));

tsk.Set(Tsk.ConstraintDate, projectstartdate);
tsk.Set(Tsk.ConstraintType, ConstraintType.AsSoonAsPossible);

Console.WriteLine("After setting constraint = " + tsk.Get(Tsk.ConstraintType));
Console.WriteLine("After setting constraint date = " + tsk.Get(Tsk.ConstraintDate));

proj.Save(“outputWithConstraintType.xml”, Aspose.Tasks.Saving.SaveFileFormat.XML);

Thank you for the code. But with the given sample code the start and finish dates of the task is 1/1/2016 and 1/1/2016. I want the task dates to show as 1/1/2016 and 31/12/2016

Hi Purnima,


The recommended method to set task parameters is to use task start date and duration. It will make no change in the constraint type and date as well. You may please use following code where working days in the year are used as duration and share the feedback.



Project proj = new Project();

DateTime projectstartdate = Convert.ToDateTime(“2016,01,01”);

DateTime projectenddate = Convert.ToDateTime(“2016,12,31”);


proj.Set(Prj.StartDate, projectstartdate);

proj.Set(Prj.FinishDate, projectenddate);


Task tsk = proj.RootTask.Children.Add(“Task 1”);


tsk.Set(Tsk.Start, projectstartdate);

tsk.Set(Tsk.Duration, proj.GetDuration(261,TimeUnitType.Day));


proj.Recalculate();

proj.Save(“outputWithConstraintType.xml”, Aspose.Tasks.Saving.SaveFileFormat.XML);

Hi,

Thank you for the code. What should I do, if I want MPP to take calendar days as duration and not number of working days. That is, if i give 365, it should calculate 31 dec 2016, instead of 261 days.

I want the task to look like the following

Task1 1/1/2016 31/12/2016

If i get the datediff for the start and end date and set duration, it is coming as

Task1 1/1/2016 25/5/2017

Following is the code i added

tsk.Set(Tsk.Start, projectstartdate);
int datediff = Convert.ToInt16((projectenddate - projectstartdate).TotalDays);
tsk.Set(Tsk.Duration, proj.GetDuration(datediff));


Regards,

Hi Purnima,


You may please use following sample code to calculate working days between two dates for setting the duration.



Project proj = new Project(@“Blank2010.mpp”);

Task task1 = proj.RootTask.Children.Add(“Task 1”);

task1.Set(Tsk.Start, new DateTime(2016, 1, 1));


ATasks.Calendar cal = proj.Get(Prj.Calendar);

double Days24Hrs = cal.GetWorkingHours(new DateTime(2016, 1, 1), new DateTime(2016, 12, 31)).WorkingHours.Days;

double Days08Hrs = Days24Hrs * 3;


// task1.Set(Tsk.Finish, new DateTime(2016, 12, 31));

task1.Set(Tsk.Duration, proj.GetDuration(Days08Hrs, TimeUnitType.Day));


proj.Save(path + “output.mpp”,SaveFileFormat.MPP);

With the given code, I am getting the following task created in the output mpp.


Task 1 start date : 1/1/2016 end date: 30/12/2016

required output is 31/12/2016. I tried some steps… but didnt work.

secondly, can we set the task mode of a task to manually schedulled while creating it? If so, how?

Hi Purnima,


If we look into calendar, Dec 30, 2016 is Fri which is last working day as per the default calendar. That is why end date is Dec 30, 2016 rather than Dec 31, 2016. For setting task to manual mode please use following sample code.



Project proj = new Project(“sample.mpp”);

Task tsk1 = proj.RootTask.Children.Add(“Task 1”);

tsk1.Set(Tsk.IsManual, true);

I am not convinced with that, as when I give start and dates as 3 aug 2016 and 31 aug 2016, it turns out to be 3 aug 2016 and 26 aug 2016. 31aug 2016 is a wednesday and a working day.


Secondly, when i am setting the task as manual using the code that you have given, it gives a compilation error:

The type arguments for method ‘Aspose.Tasks.Task.Set(Aspose.Tasks.Key<T,Aspose.Tasks.TaskKey>, T)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.


An immediate reply would be of atmost help. THank you…

Hi Purnima,


We are sorry for the inconvenience caused to you.

You may please use cal.GetWorkingHours() such that complete start/finish date and time is mentioned. If you set finish date like 2016,12,31, it will be taken as 2016,12,31 00:00:00 and hence the day 31st will not be taken into account.

Following sample code is tested for different dates and working fine.

<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>Project proj = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Project(@“Blank2010.mpp”);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>Task task1 = proj.RootTask.Children.Add(“Task 1”);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>task1.Set(Tsk.IsManual, <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>true<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>DateTime startDate = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> DateTime(2016, 8, 3,8,0,0);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>DateTime finishDate = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> DateTime(2016, 8, 31,17,0,0);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>task1.Set(Tsk.Start, startDate);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>ATasks.Calendar cal = proj.Get(Prj.Calendar);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>double<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> totalHours = cal.GetWorkingHours(startDate, finishDate).WorkingHours.TotalHours;<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>task1.Set(Tsk.Duration, proj.GetDuration(totalHours, TimeUnitType.Hour));<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>proj.Save(“output2.mpp”, SaveFileFormat.MPP);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>
I have also tried task.Set(Tsk.IsManual, true) and found it working fine. Could you please share the feedback again after testing above code?

Hi,


When I write the same code given by you, IsManual is showing compile time error(as shown in the attached image file, the line of code is showing red underlined).

Following is the error:

The type arguments for method ‘Aspose.Tasks.Task.Set(Aspose.Tasks.Key<T,Aspose.Tasks.TaskKey>, T)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.

Hi Purnima,


Are you using some old version of the API? We are not able to reproduce this issue at our end. Please create a sample console application with such a problem and provide to us so that we can use to investigate the issue further at our end.

Thank you for the quick reply. Pls find attached solution.

Hi Purnima,


We have tested your project and couldn’t find any issue as you have mentioned. Please have a look at the this screenshot where we have opened your project and compiled it successfully without any changes. Please clean your build and rebuild your solution to see if it gets through.