In Lightning component I have attribute and input for date:
<aura:attribute name="myDate" type="Date"/>
<!-- ... -->
<ui:inputDate aura:id="my-date" labelClass="slds-form-element__label" class="slds-input" displayDatePicker="true" label="my Date" value="{!v.myDate}"/>
In Apex controller code I have method that receives Date as param:
@AuraEnabled
public static void testDate(Date myDate) {
System.debug(myDate);
}
In component controller I have code that takes attribute value and sets it as action parameter:
var apexTestDate = component.get("c.testDate");
apexTestDate.setParams({
myDate: component.get('v.myDate')
});
apexTestDate.setCallback(this, function(response) { console.log(response); });
$A.enqueueAction(apexTestDate);
In Apex date is null:
System.debug(myDate); // 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?
SOLUTION
/* js */ myDateJSON: new Date(component.get('v.myDate')).toJSON()
/* Apex */ Date myDate = Date.valueOf(myDateJSON /* String */ );
JSON
string andparse
it inApex
.