0

I have a custom field vaule in WordPress. This field is for the year of birth, for example 1980. I would like to convert this year to display the age of (in the example) 33.

Furthermore, I would like to increase this number once every year. So next year, the age till be 34.

Is this possible to do with javascript?

Best regards, Fredrik

2 Answers2

0

This way:

function getAge(yearOfBirth) {
  return (new Date()).getFullYear() - yearOfBirth;
}
dseminara
  • 11,665
  • 2
  • 20
  • 22
0

So just get the current date with JS, and substract the base year from it:

var base_year = 1980; //get this from DB

var today = new Date(); 
var current_year = today.getFullYear(); //2014 

alert (current_year - base_year); // = 34
d.raev
  • 9,216
  • 8
  • 58
  • 79