1

I have to check the age validation, so I have tried the following method for getting age:

let newDate = new Date(this.userDob);
var timeDiff = Math.abs(Date.now() - newDate);
let age = Math.floor((timeDiff / (1000 * 3600 * 24)) / 365);

here this.userDob is a string. How can I use this string to get age using javascript?

Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59

1 Answers1

1

I have done it using like this,

var dob = //Here Im getting dob
var today = new Date();
var birthDate = new Date(dob);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    age--;
}

Now I'm able to check the age

Averla Team
  • 407
  • 1
  • 6
  • 16