9

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 */ );
1
  • 4
    As of now you can't, you have to send JSON string and parse it in Apex. Apr 12, 2017 at 10:21

1 Answer 1

10

I'm afraid you won't be able to do it. As @itzmukeshy7 said, the way to go is to pass the Date as a String and then cast it inside your Apex method. There are several issues like this currently in the framework, I've published a blog post to try to list them and their related workaround, you can have a look here if you're interested.

I would suggest that you create a Case on Salesforce side for that. The more Cases related to this issue, the more likely they are to fix it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .