Skip to main content
Tweeted twitter.com/StackSalesforce/status/852535840341405696
added 116 characters in body
Source Link
Vladyslav K
  • 2.8k
  • 7
  • 34
  • 71

In Lightning component I have attribute and input for date:

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

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

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

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

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

In Apex date is null:

System.debug(paymentDatemyDate); // 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 */ );

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?

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 */ );
Source Link
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?