Elapsed Time using JavaScript (Beginner)

2158 views javascript

I have a small piece of code that I want to accomplish 3 things on:

  1. User Enters Birthday
  2. User is able to see current date
  3. When user clicks button, they are given the time elapsed between their birthday and now (i.e. current time - birthday).

The trouble I am having is that when the button is clicked all I get is NaN. I am not sure how to proceed at this point.

        <li><label>Enter Your Birthday</label></li>
        <li><input type="date" name="field1" id="field1"></li>

        <li><label for="date">Current Date</label></li>
        <script id="curDate">
            var d = new Date();
            var bDay = new Date(document.quiz.field1);
            var eTime = d - bDay;
            function calcDate(eTime) {
        <li><input type="button" value="Click" onclick="calcDate(eTime)"></li>

answered question

new Date(document.quiz.field1) <-- field1 is the input. So you are trying to convert the input to a number, not its value

1 Answer


How about this

var diff = Math.abs(new Date() - new Date(document.getElementById('field1').value));

posted this

Have an answer?


Please login first before posting an answer.