+ Start a Discussion
Luke Higgins 22Luke Higgins 22 

Lightning component that adds 30 days to an lightning:input date?

I'm trying to figure out how to add X amount of days to a date through the lightning component controller. Here is what I have been trying:

.cmp - 
<aura:component implements="flexipage:availableForAllPageTypes">
        <aura:attribute name="dataInValue" type="Date" />
        <aura:attribute name="dataEndValue" type="Date"/>

        <lightning:input aura:id="startDate" type="date" label="Enter Date" value="{!v.dataInValue}" onchange="{!c.validDate}"/>
        <br/>
        <lightning:input aura:id="endDate" type="date" label="Day they get benefits:" value="{!v.dataEndValue}"/>
        
</aura:component>
.js - 
({
    validDate : function(component,event,helper){
               var data_in = component.get("v.dataInValue");
               var data_out = formatDate(data_in) + 30;
               component.set("v.dataEndValue", data_out);
    }
})
Best Answer chosen by Luke Higgins 22
Alain CabonAlain Cabon
Another solution:
 
({
    validDate : function(component,event,helper){
        var data_in = component.get("v.dataInValue");
        var data_out = new Date(data_in);
        data_out.setDate(data_out.getDate() + 30);
        component.set("v.dataEndValue",data_out.toISOString());
    }
})

All Answers

Ankit RathorAnkit Rathor
Hi Luke,
I did walk through your code and found that you are trying to add days directly to the "dataInValue" attribute which is not compatible with JavaScript date, Please try below code in Controller.js:

 validDate : function(component,event,helper){
        var outdate = component.get("v.dataInValue");
        outdate = outdate.split("-");
        outdate = new Date(outdate[0], outdate[1], outdate[2]);
        outdate.setMonth(outdate.getMonth() + 1);
        component.set("v.dataEndValue", outdate.getFullYear() + "-" + outdate.getMonth() + "-" + outdate.getDate());
    }


Please let me know if you have any other query. If this solution is helpful then please mark it as Best Answer.

Thanks,
Ankit Rathor 
Alain CabonAlain Cabon
Another solution:
 
({
    validDate : function(component,event,helper){
        var data_in = component.get("v.dataInValue");
        var data_out = new Date(data_in);
        data_out.setDate(data_out.getDate() + 30);
        component.set("v.dataEndValue",data_out.toISOString());
    }
})
This was selected as the best answer