function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gopal ChatGopal Chat 

Auto Populate Date of birth by age In Lightning Component

When i input age in lightning input,date of birth will be auto populate with 1 jan of the year
For example -when i set 2 in years the age,date of birth will be 2 jan 2017User-added image
NitishNitish
Hi Gopal,

Find the below code:
.cmp file
<aura:component access="global" >
	
    <aura:attribute name="dateOfBirth" type="Date"/>
    <aura:attribute name="age" type="Integer"/>
    <lightning:input type="Date" 
                     label="Date Of Birth"
                     required="true"
                     value="{!v.dateOfBirth}"/>
    <lightning:input name="text" label="Age" 
                     onkeyup="{!c.calculateDOB }"
                     value="{!v.age}"
                     format="MM/dd/yyyy"/>
    
    
</aura:component>
controller.js
({
    calculateDOB : function(component, event, helper) {
        var ageNo = component.get("v.age");
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth(); //January is 0!
        var yyyy = today.getFullYear();
        var dob=new Date(yyyy-ageNo,mm,dd);
        component.set("v.dateOfBirth",dob);
        console.log(dob);
    }
})

Let me know if you have any doubt.

Thanks,
Nitish