Skip to main content
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 2017Auto Populate Date of birth by age In Lightning Component
1 answer
  1. Feb 11, 2019, 3:14 PM
    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

     
0/9000