​​​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:

  1. When you choose Weekly, the Date2 become equals to Date1 + 7.
  2. When you choose Monthly, the Date2 become equals to Date1 + 31.
  3. When you choose Yearly, the Date2 become equals to Date1 + 365.

Here are the steps on how to do that:

  1. Open the form in design mode.
  2. Add the following to the form design workspace:
    How to add days to a specific date based on condition.jpg
  3. Select Date1.
  4. In the Rules pane, click on the Add icon to add a new rule, the Rule Manager dialog will open.
    How to auto-shifting Panel control in a form2.jpg
  5. 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.
  6. Select DropDownList1.
  7. In the Rules pane, click on the Add icon to add a new rule, the Rule Manager dialog will open.
    How to auto-shifting Panel control in a form2.jpg
  8. 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.
  9. Test the form by clicking on the Preview button in the View Group.