Answers for "Create a JavaScript Function with the name age () this function will take your date of birth as parameters and return your age in years."

4

javascript work out age from date of birth

// To calculate age:
var year_born = prompt("Please enter your date of birth:", "Type here");
var d = new Date();
var n = d.getFullYear();
function getAge(birthYear){
	var currentDate = new Date();
    var currentYear = currentDate.getFullYear();
    age = currentYear - birthYear;
    return age;
}
calculatedAge = getAge(year_born);
alert("Hello, " + "you are " + calculatedAge + " years old!");
Posted by: Guest on November-03-2020
1

how to code a check age function in javascript

function Person(dob) {
  // [1] new Date(dateString)
  this.birthday = new Date(dob); // transform birthday in date-object
  
  this.calculateAge = function() {
    // diff = now (in ms) - birthday (in ms)
    // diff = age in ms
    const diff = Date.now() - this.birthday.getTime(); 
    
    // [2] new Date(value); -> value = ms since 1970
    // = do as if person was born in 1970
    // this works cause we are interested in age, not a year
    const ageDate = new Date(diff); 
    
    // check: 1989 = 1970 + 19
    console.log(ageDate.getUTCFullYear()); // 1989
    
    // age = year if person was born in 1970 (= 1989) - 1970 = 19
    return Math.abs(ageDate.getUTCFullYear() - 1970);
  };
}
var age =new Person('2000-1-1').calculateAge();
console.log(age); // 19
Posted by: Guest on September-03-2020
0

Create a JavaScript Function with the name age () this function will take your date of birth as parameters and return your age in years.

"june is 11 "
Posted by: Guest on August-25-2021
0

Create a JavaScript Function with the name age () this function will take your date of birth as parameters and return your age in years.

'june 11"
Posted by: Guest on August-25-2021
0

Create a JavaScript Function with the name age () this function will take your date of birth as parameters and return your age in years.

function test(name, birth_year, current_year) {
  const age = current_year - birth_year;
  const response = name + " is " + age;
  return response;
}

test("bob", 1906, 1965)
Posted by: Guest on August-25-2021
0

Create a JavaScript Function with the name age () this function will take your date of birth as parameters and return your age in years.

function test(name, birth_year, current_year) {

  const age = current_year - birth_year;

  const response = name + " is " + age;

  return response;

}

test("john", 1894, 1917)
Posted by: Guest on June-27-2021

Code answers related to "Create a JavaScript Function with the name age () this function will take your date of birth as parameters and return your age in years."

Code answers related to "Javascript"

Browse Popular Code Answers by Language