Skip to main content
1 of 2
Vladyslav K
  • 2.8k
  • 7
  • 34
  • 71

How to pass Date from Lightning to Apex

In Lightning component I have attribute and input for date:

<aura:attribute name="paymentDate" type="Date"/>
<!-- ... -->
<ui:inputDate aura:id="payment-date" labelClass="slds-form-element__label" class="slds-input" displayDatePicker="true" label="Payment Date" value="{!v.paymentDate}"/>

In Apex controller code I have method that receives Date as param:

@AuraEnabled
public static void testDate(Date paymentDate) {
    System.debug(paymentDate);
}

In component controller I have code that takes attribute value and sets it as action parameter:

var apexTestDate = component.get("c.testDate");
apexTestDate.setParams({
    paymentDate: component.get('v.paymentDate')
});
            
apexTestDate.setCallback(this, function(response) { console.log(response); });
$A.enqueueAction(apexTestDate);

In Apex date is null:

System.debug(paymentDate); // DEBUG|null

I can walk-around this by converting date to long and passing number to apex and convert to date again, but this is not best solution indeed. Can somebody suggest how to solve this?

Vladyslav K
  • 2.8k
  • 7
  • 34
  • 71