0

I am pulling a date of birth as a string from a form on a website im looking after. I need to be able to derive an age from that date string. My thoughts are converting it into a proper date using a split (its delimited by "%2F") and calculating from that but my syntax isnt that good so im having real trouble. The code im working with to pull the string I need is;

function() {
var inputField = document.getElementById("date-of-birth-input");
return inputField.value || "";
}

Any help would be appreciated.

DTVDB
  • 1
  • 1
  • Start with [*Why does Date.parse give incorrect results?*](http://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results), then to calculate age perhaps [*JavaScript calculate years and days from start date*](http://stackoverflow.com/questions/11570232/javascript-calculate-years-and-days-from-start-date). – RobG Jan 31 '17 at 04:08

2 Answers2

0

If you have the user input the date in a format like 1/1/1990, you could do something like this:

var inputField = document.getElementById("date-of-birth-input"); // `1/1/1990`
var today = new Date().getFullYear();
var birthdate = new Date(inputField).getFullYear();
var age = today - birthdate;

The JavaScript Date constructor is pretty flexible in what it can turn into a date, so you might not need to go through splitting-string hassles. Check out the docs.

The Qodesmith
  • 3,205
  • 4
  • 32
  • 45
  • This relies on unreliable parsing of the Date constructor to create a date. Also, the age calculation will likely be incorrect given that a person's age is not simply the difference in the year value (e.g. someone born on 31 Dec 2016 is not 1 year old on 1 Jan 2017). – RobG Jan 31 '17 at 05:00
  • @RobG As far as the Date constructor parsing unreliably, my example will always parse reliably since I used the format `1/1/1990`. I also pointed to the docs so the OP could discover other ways to play with the constructor. – The Qodesmith Jan 31 '17 at 19:09
  • 1
    Please see MDN [*Date.parse*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) and [*Date*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). – RobG Jan 31 '17 at 20:24
  • Thanks for the replies. I did something similar to this. Except I split the date string into an array and created a calculation for the no of months and days as a % of the year (just averaged 30.14 days per month) to get the number of years plus .xxxxx. Then adjust with another -0.05 to adjust for leap year etc. Its accurate down to within a day which for what I need to record is perfect. – DTVDB Feb 01 '17 at 04:52
0

Date format varies in different countries. In US the date format is MM-DD-YYYY while in Asian countries the date format is DD-MM-YYYY and in China its 'YYYY-MM-DD'. The right way to go about dealing with dates is using Moment.js

If you have a simple application/website include moment in your <head> tag as follows

<script src="moment.js"></script>

(Hoping you have downloaded moment.js and kept in the same structure as index.html). Otherwise use this if you want to use it directly from the net without downloading.

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/locale/af.js"></script>

Then use the following

   function() {
    /* First-make sure you format moment according to your giving locale.
      I'm formating here according to the US date format */
    var inputField = moment(document.getElementById("date-of-birth-input")).format("MM-DD-YYYY");
    return moment().diff(inputField, 'years');
    }

I hope this helps. Thanks.

Manish Poduval
  • 452
  • 3
  • 15
  • The OP has not noted moment.js as a tag or in the question, so your answer should not rely on it. When parsing a date string, you should always tell the parser the format, otherwise you might as well use *Date.parse* (which is not recommended). There is nothing in this answer that the OP could not have learnt from the moment.js documentation, so it should just be a comment. – RobG Jan 31 '17 at 04:58