How to add days to a specific date based on condition?
Last Update: November 22, 2017
Suppose you have a form contains a dropdown list with values: Weekly, Monthly and Yearly and you have two date controls (Date1 and Date2), you want to achieve the following:
- When you choose Weekly, the Date2 become equals to Date1 + 7.
- When you choose Monthly, the Date2 become equals to Date1 + 31.
- When you choose Yearly, the Date2 become equals to Date1 + 365.
Here are the steps on how to do that:
- Open the form in
design mode.
- Add the following to the form design workspace:

- Select Date1.
- In the
Rules pane, click on the Add icon to add a new rule, the
Rule Manager dialog will open.

- Add a new rule as follows:
A. Change the
Rule Name as desired.
B. Change the
Rule Type to Action.
C. Change the
Event Type to onChange. Leave "Execute if event triggered only on this control" unchecked.
D. In the
Action section, type the following script:
var d = new Date();
setValue(Date1, d.toString())
E. Click on the
Save button to save the rule.
Note: You can use
Assistance Panel to help you adding functions and related parameters, form variables and form controls to
Conditions and/or
Actions sections. - Select DropDownList1.
- In the
Rules pane, click on the Add icon to add a new rule, the
Rule Manager dialog will open.

- Add a new rule as follows:
A. Change the
Rule Name as desired.
B. Change the
Rule Type to Action.
C. Change the
Event Type to onChange. Leave "Execute if event triggered only on this control" unchecked.
D. In the
Action section, type the following script:
switch(getValue(DropDownList1)){
case "Weekly":
setValue(Date2, addDateTime(getValue(Date1),'Days','7'));
break;
case "Monthly":
setValue(Date2, addDateTime(getValue(Date1),'Days','31'));;
break;
case "Yearly":
setValue(Date2, addDateTime(getValue(Date1),'Days',365));
break;
}
E. Click on the
Save button to save the rule. - Test the form by clicking on the
Preview button in the
View Group.