title: Calculate age from date of birth user input value in react
body: <p>I am trying to fetch user input date of birth value and convert to its equivalent age.</p>
  
  <p>In the console showing the fetched value, but its not assigned to state variable <code>dob1</code>, struck with age calculation function execution.</p>
  
  <p>The entire code is available in the stackblitz and it's link: <a href="https://stackblitz.com/edit/react-geum6v?file=index.js" rel="nofollow noreferrer">https://stackblitz.com/edit/react-geum6v?file=index.js</a></p>
  
  <p>HTML</p>
  
  <pre><code>Number of male over certain age: &lt;input type="date" name="date_of_birth" defaultValue= {dob1} onChange={this.handleChange_age}&gt;&lt;/input&gt; &lt;br /&gt;&lt;br /&gt;
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>handleChange_age = (event) =&gt; {
      console.log("DOB:", event.target.value);
  
      this.setState({ dob1: event.target.value })
      console.log(this.state.dob1);
      var age_latest = {age_latest: this.calculate_age}
      console.log(age_latest);
  
      this.setState({ age1: age_latest })
      console.log("Age:", this.state.age1);
    }
  
  calculate_age = (dob1) =&gt; {
      var today = new Date();
      var birthDate = new Date(this.state.dob1);
      var age_now = today.getFullYear() - birthDate.getFullYear();
      var m = today.getMonth() - birthDate.getMonth();
      if (m &lt; 0 || (m === 0 &amp;&amp; today.getDate() &lt; birthDate.getDate())) 
      {
          age_now--;
      }
      console.log(age_now);
      return age_now;
    }
  </code></pre>