Web Analytics Made Easy -
StatCounter Simple Age Calculator - CodingForum

Announcement

Collapse
No announcement yet.

Simple Age Calculator

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple Age Calculator

    Hi there,

    I'm not too good at dealing with dates in javascript. What I want to do is create a simple function to return a person's age in years only, based on their birthday date passed into the function. Does anybody have this code to hand or have a suggestion about how to approach it.

    Thanks for your help

    Chris

  • #2
    Code:
    function getAge(dateStr) {
    	var now = new Date();
    	var birthdate = new Date(dateStr);
    
    	if(birthdate.getTime() > now.getTime())
    		return 0;
    
    	var diff = now.getTime() - birthdate.getTime();
    	return Math.floor(diff / (1000*60*60*24*365));
    }
    dateStr is a string for the day of birth. An example is:
    January 1, 1970

    Hope that helps!

    Happy coding!

    Comment

    Working...
    X