Skip to main content
Legacy User
April 2, 2021
Solved

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.

  • April 2, 2021
  • 8 replies
  • 136 views
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.
Best answer by Community Expert

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;

8 replies

Docusign Employee
April 6, 2021

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;

Legacy User
April 6, 2021

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.

Legacy User
April 6, 2021

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

Legacy User
April 6, 2021

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

Legacy User
April 16, 2021

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?

Legacy User
April 24, 2021

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

Legacy User
May 3, 2021

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")))

Justin Jiang
Established Contributor
Established Contributor
April 25, 2024

Very useful!

Welcome to the DocuSign Community and University! Your feedback is immensely appreciated. If this answer resolves your issue, please feel free to give it a Like and mark it as the "Best Answer". This helps others in the community find the information they need. Visit us at http://dsucustomers.docusign.com for more resources and support.