Issue
I need to ensure that the End Date inputted is at least one day after the Start Date. However, if the Start Date hasn't been entered yet, the End Date should be allowed to be any date after today. I'm wondering if using {{input_startDate.selectedDate ? moment(input_startDate.selectedDate).add(1, ‘d’).format() : moment().format()}} in the Min Date property would work for this.
Resolution
When working with date validation in Appsmith, you may want to enforce a rule where the End Date must be at least one day after the Start Date — but still allow flexibility when the Start Date hasn’t been selected yet.
Here’s how you can handle this using the Min Date property of your End Date input field.
Scenario 1: Enforce a minimum date relative to Start Date or today
If the Start Date is selected, you want the End Date to be at least one day later.
If not, allow any End Date after today’s date.
You can use the following code in the Min Date property:
{{ input_startDate.selectedDate
? moment(input_startDate.selectedDate).add(1, 'd').format()
: moment().format()
}}Explanation:
- The code checks if
input_startDate.selectedDateexists. - If it does, it adds one day using
moment().add(1, 'd'). - If not, it defaults to today’s date (
moment().format()).
This ensures the user can’t pick an End Date earlier than today if no Start Date is defined.
Scenario 2: Allow any End Date when Start Date is empty
If you want no restriction at all when Start Date isn’t provided, you can leave the minimum date blank:
{{ input_startDate.selectedDate
? moment(input_startDate.selectedDate).add(1, 'd').format()
: ""
}}Explanation:
- If Start Date exists, the minimum End Date is one day after it.
- If Start Date doesn’t exist, the Min Date is empty, meaning there’s no restriction.
Notes
- Ensure that your DatePicker widget names (
input_startDate,input_endDate) match your setup. - Appsmith supports Moment.js for date operations in bindings.
- You can use similar logic for Max Date validation if needed.