We can’t directly pass the Date field value from Lightning Component to its Apex Controller. What we need is to:
- Capture the value of Date in a String variable in Apex function’s parameter.
- Convert that String value to Date value.
- Use the Date value where we want to.
Example :
DateUsage.cmp
<aura:component> <ui:inputDate displayDatePicker="true" aura_id="expDate" format="MM-DD-YYYY"/> </aura:component>
DateUsage.js
({ createExpDate: function(component, event,helper) { var expirationDate = component.find("expDate").get("v.value"); var action = component.get("c.createExpDate"); action.setParams({ expDate : expirationDate }); // callback and enqueue action } })
DateUsageController.cls
public class DateUsageController { @AuraEnabled public static void createExpDate(String expDate) { Date expirationDate = Date.valueOf(expDate); // Now we can use expirationDate } }
I hope this solution works for you to pass the Date field from Salesforce Lightning Component to Apex Controller.
Leave a comment