1

I have an API that returns a users dob as a variable called dateOfBirth which comes from the API like so:

{
    "dob": 1525219200
}

so now dateOfBirth is simply:

1525219200

How do I get the users age as an Int from the above.

I have tried:

var dob = 1525219200;
var now = Date.now();

var age = new Date();
age = (dob - now).toUTCString()

document.write("dob: " + dob + "<br/>");
document.write("now: " + now + "<br/>");
document.write("age: " + age + "<br/>");

However, I am a junior dev and I think I am not understanding correctly what it is I am doing. Any and all help will be gratefully appreciated.

Here is a JSFIDDLE

modusTollens
  • 397
  • 7
  • 23
  • For the age, you don't need to assign as date, then reassign. Try `age = new Date(dob - now)` – colecmc May 29 '18 at 15:34
  • The number appears to be a UNIX time value. To convert to a javascript Date, multiply by 1000 and use the Date constructor: `new Date(1525219200 * 1000)`. To get a person's age from a date, see the duplicate (there are many answers, some better than others). – RobG May 30 '18 at 04:45

2 Answers2

1

I suppose that's milliseconds. Then you will need to get today's date in milliseconds:

todayInMilli = Math.round(new Date() / 1000);

Next you need to find the difference between today and DOB in milliseconds:

ageInMilli = todayInMilli - dob;

Finally convert milliseconds to years:

age = ageInMilli / (60 * 60 * 24 * 365)

please let me know if this worked for you

https://jsfiddle.net/h4a8m7a5/

RoRFan
  • 344
  • 4
  • 8
  • This is a fairly rough approximation as it doesn't account for leap years. – RobG May 30 '18 at 04:48
  • This doesn't seem to work. Have a look: https://jsfiddle.net/qtsLq4dt/2/ - it says the age is 48 but anyone born in 1988 will be either 29/30 depending on the month. – modusTollens May 30 '18 at 08:49
  • I updated my answer and added jsfiddle too. It should now work @Mark – RoRFan May 30 '18 at 10:48
  • 1
    Thank you. :) I have accepted the answer. It would be even more awesome if you could add comments and explain what it is doing and why. As a junior I need to learn not just copy code and I think it will also help others. – modusTollens May 30 '18 at 12:05
  • @Mark—for an accurate forumula, see [*this answer*](https://stackoverflow.com/questions/4060004/calculate-age-given-the-birth-date-in-the-format-yyyymmdd/22562497#22562497) to the duplicate question. – RobG May 31 '18 at 11:25
1

var dob = 1525219200;
var now = Date.now();

var age = new Date();
age = moment.duration(now - dob).years();


console.log("dob: " + moment(dob).format('DD/MM/YYYY'));
console.log("now: " + moment(now).format('DD/MM/YYYY'));
console.log("age: " + age + " years");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>

You need to use moment js. Do not try to reinvent the weel.

var dob = 1525219200;
var now = Date.now();

var age = new Date();
age = moment.duration(now - dob).years();


console.log("dob: " + moment(dob).format('DD/MM/YYYY'));
console.log("now: " + moment(now).format('DD/MM/YYYY'));
console.log("age: " + age + " years");
  • This treats 1525219200 as equivalent to 18 Jan 1970, however it more likely should be multiplied by 1000 (to convert seconds to milliseconds). But the OP really should clarify that. – RobG May 30 '18 at 04:51