Skip to main content
I want to have on an input form a start date and a number of months field and then calculate the end date to be that number of months after the start date. The end date will also be an attribute like start date and increment number of months.

Hello, you can definitely achieve this by using C# expression to calculate the End Date.

Assuming you have all 3 as Var (2 date type and 1 num), in an Update Variable step the End Date var will have this value

To add any number of days to a provided date

//Get the variable containing your date/time, parse it, and add or subtract days indicated (by indicating a positive or negative number)

DateTime.Parse(GetVariableValue("StartDate")).AddDays(GetVariableValue("NoOfDays"))

Remember: the expression above does not take into account just business working days. in that case i would use this expression to calculate it:

//Create a new variable called 'current', and set it to the system dateTime for today

var current = System.DateTime.Today;

//Create a variable called 'daysToAdd' and load it with the number from your workflow variable

var daysToAdd = Int32.Parse(GetVariableValue("YOUR_VARIABLE").ToString());

//Add days until the number of indicated days is reached   

for (var i = 0; i < daysToAdd; i++) {  

if (current.DayOfWeek != DayOfWeek.Friday) {    

current = current.AddDays(1);  

else {    

current = current.AddDays(3);  

}

}

return current;


Wow, Thank you so much for this answer. I will try it today and then update this post again. Your explanation is so clear and easy to follow too.


I get this error. StartDate is a Date, Interval is a Num, EndDate which is this variable I am setting the expression below to is a Date. I tried with ; and without but I know that could not be it. I did set StartDate above as System.DateTime.Today;

Compilation failed: (1:71) Argument 1: cannot convert from 'string' to 'double' - Reference Id: d9a9665f-d171-427f-86d8-f4a8636aeaa5


Hey I think the issue is that DateTime.Parse(GetVariableValue("StartDate")).AddDays(GetVariableValue("NoOfDays")) The part in bold wants to return a string and so I need to know how to add the part that converts NoOfDays back into a number


Hello, i know there is a way to turn strings into numbers and viceversa in C# with Convert. e.g Convert.ToInt32("string")

however, I was under the impression that NoOfDays was a number Var. if not, why don't you make it consisted my converting the attribute into a NUmber Var?


I still can't figure this out 

Getting this error. I have StartDate as a Date Variable and NumberofDays as a number and I am trying to set a Date variable.image


Resolved this in another thread and confirmed it worked:​

If it’s complaining that GetVariableValue("NumberOfDays") is a String rather than a Number, could you try forcing it to an Int?

DateTime.Parse(GetVariableValue("StartDate")).AddDays(Int32.Parse(GetVariableValue("NumberOfDays")))


Very useful!


Reply