Need to calculate age from date of birth

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1730
    rabinsky_9269
    Participant

    Hello, basically what the title says.

    I would like to have a code that I can input in the about me page to show my age in realtime.

    For example, I’m John and I am [code here] years old.

    Google search did not really help as most codes are not very user friendly and need separate plugins. I just want to run it inside of a block.

    ​

    Thanks!

    #1731
    ResearchMan123
    Guest

    function _calculateAge(birthday) { // birthday is a date
    var ageDifMs = Date.now() – birthday.getTime();
    var ageDate = new Date(ageDifMs); // miliseconds from epoch
    return Math.abs(ageDate.getUTCFullYear() – 1970);
    }

    const dateOfBirth = new Date(’11/15/1995′);
    console.log(_calculateAge(dateOfBirth) + ” years old”);
    just put whatever your date of birth is in the date of birth variable, and then instead of console log just use it to change whatever text you want

    #1732
    greg8872
    Guest

    This made me think of this…. https://www.youtube.com/watch?v=qHDSNs9wBpU&feature=youtu.be&t=68

    While not as picky as the clip, things like the location of the viewer (and if you are calculating server side, where the server is located) can be a little tricky that 24 hour period around midnight of their birthday.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.