0

Can't seem to find the problem. Every time I run it I get NaN for ageDale, been looking at it for a while now, its probably simple but I appreciate the help!

<p>Enter names in the fields, then click "Submit" to submit the form:</p>
<form name="form">
    <input type="text" id="birthDate">

    Current Date
    <input type="text" id="currentDate">

    <a id="Submit_Button" onclick="test();" href="javascript:void(0);" title="Submit">Submit</a>
</form>
<script>
    function test() {
        var birthDate   = document.getElementById("birthDate");
        var currentDate = document.getElementById("currentDate");
        var ageDate     = (birthDate.value - currentDate.value);

        if(ageDate.value < 1) {
            (ageDale =  ageDate.value * -1)
        }
        else {
            (ageDale = ageDate.value * 1)
        }

        alert(ageDale)
    }
    </script>

Also, is it necessary for me to have that else statement? or is there another way to set up this so its not needed?

msturdy
  • 10,479
  • 11
  • 41
  • 52
  • 3
    You misspelled the variable name. Try `ageDate` instead of `ageDale`. – orange Dec 11 '14 at 01:14
  • 2
    You didn't say what the code is supposed to do but your NaN is coming from using the subtraction operator on two strings `(birthDate.value - currentDate.value)` – styfle Dec 11 '14 at 01:15
  • 1
    @styfle JS will convert strings to numbers when you use arithmetic functions. – Barmar Dec 11 '14 at 01:16
  • 1
    And you recreated [`Math.abs()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs) – epascarello Dec 11 '14 at 01:18

4 Answers4

2

This

ageDate.value

should be

ageDate

only. It's a variable and already contains only the difference from

birthDate.value - currentDate.value

if(ageDate.value < 1) {
         // ^ here
            (ageDale =  ageDate.value * -1)
        }                      //^ here
        else {
            (ageDale = ageDate.value * 1)
                             // ^ and here

You only need to fetch the value when getting data from, for example, input fields.

Also (depending on how you input them) it might be a problem to calculate dates. For debugging purposes you should

console.log()

your variable values, that way you will find out quickly where the error is.

A good place for a console.log() in your code would be, for example after this block:

    var birthDate   = document.getElementById("birthDate");
    var currentDate = document.getElementById("currentDate");
    var ageDate     = (birthDate.value - currentDate.value);

    console.log(ageDate);

SIDENOTE:

You might want to take a look at moment.js, which will help you with date calculations. For example, you can get differences between dates with moment.js like this:

var a = moment([2014, 12, 05]);
var b = moment([2014, 12, 06]);
a.diff(b, 'days') // 1
baao
  • 71,625
  • 17
  • 143
  • 203
1

Try this:

var btn = document.getElementById("Submit_Button");

btn.onclick = function test() {
    var birthDate   = parseInt(document.getElementById("birthDate").value);
    var currentDate = parseInt(document.getElementById("currentDate").value);
    var ageDate     = (birthDate - currentDate);

    if(ageDate < 1) {
        (ageDate =  ageDate * -1)
    }
    else {
        (ageDate = ageDate * 1)
    }

    alert(ageDate)
}
Sergio Flores
  • 5,231
  • 5
  • 37
  • 60
1

As baao said, you have spelling errors. After correcting those, you want to consider what your input is going to be, and make sure you are checking that the input is valid.

For example, if I type "September 10th" for my birthday and "December 10th" for the current date, your function will try and subtract two strings which is not valid. If you're going to use a custom input field for the date, you need to be sure its in a consistent and parseable format.

I'd recommend asking for just their birthday in a specific format and parsing it from there, since we can use Javascript to get the current date easily. For example, mm-dd-yy. We may re-write it as:

function test() {

//lets get the date, in the format 'mm-dd-yy'. You'd want to do error checking at some point if you're serious about it
  
var dateInput = document.getElementById("birthDate").value;

//get each individal date type by splitting them at the -, giving ['dd', 'mm', 'yy']
var dateSplit = dateInput.split('-');
   
    //create a Javascript date object with the date we got
    var birthDate = new Date(dateSplit[2], dateSplit[0], dateSplit[1]);
    
    //create another with the current date and time
    var currentDate = new Date();
    
    // find the difference in milliseconds
  
   var dateDifference = Math.abs(birthDate.getTime() - currentDate.getTime());
  
   // convert to years
   var age = dateDifference / (1000 * 3600 * 24 * 365); 
  
  alert(age);

}
<p>Enter names in the fields, then click "Submit" to submit the form:</p>
<form name="form">
    Birth Date (dd-mm-yy):
  <br>
    <input type="text" id="birthDate">
  <br>
    <a id="Submit_Button" onclick="test();" href="javascript:void(0);" title="Submit">Submit</a>
</form>
Aweary
  • 2,302
  • 17
  • 26
0

just modify this code

    var birthDate   = document.getElementById("birthDate");
    var currentDate = document.getElementById("currentDate");
    var ageDate     = (birthDate.value - currentDate.value);

    if(ageDate.value < 1) {
        (ageDale =  ageDate.value * -1)
    }
    else {
        (ageDale = ageDate.value * 1)
    }

with this

    var vbirthdate = new Date(document.getElementById("birthDate").value);
    var vcurrentdate = new Date(document.getElementById("currentDate").value);
    var ageDate     = Math.floor((vbirthdate-vcurrentdate)/(1000*60*60*24));

    if(ageDate < 1) {
        (ageDate =  ageDate * -1)
    } // no need to do something like this (ageDate *1) if it is already a positive number, just check if it's a negative then convert it to a positive number

you can try the code at http://jsfiddle.net/kapasaja/duco4cqa/5/

what you asking is similar to this post

Community
  • 1
  • 1
Hilarius L. Doren
  • 757
  • 1
  • 12
  • 29