Birthday Calculation (v17)

Revision 17 of this benchmark created on


Setup

var dateString = "13.01.1994";

Test runner

Ready to run.

Testing in
TestOps/sec
Kristoffer Dorph comparing anothers code
getAge(dateString);

function getAge(dateString) {
  var today = new Date();
  var birthDate = new Date(dateString);
  var age = today.getFullYear() - birthDate.getFullYear();
  var m = today.getMonth() - birthDate.getMonth();
  if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    age--;
  }
  return age;
}
ready
Kristoffer Dorph
calcAge(dateString);

function calcAge(dateString) {
  var birthday = +new Date(dateString);
  return~~ ((Date.now() - birthday) / (31557600000));
}
ready
André Snede's Answer
calcAge(dateString);

function calcAge(dateString) {
  var birthday = new Date(dateString);
  var ageDifMs = Date.now() - birthday.getTime();
  var ageDate = new Date(ageDifMs); // miliseconds from epoch
  return Math.abs(ageDate.getFullYear() - 1970);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.