javascript to calculate the age

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    javascript to calculate the age

    Hii ,
    I have a field where the user enters his date of birth and after this i need to calculate his/or age ..how to do it.



    thanks,
    Pradeep
  • Rsmastermind
    New Member
    • Sep 2008
    • 93

    #2
    Please let me know the format of the of the date of birth field so that we can trace out the day,month and year separeately then after the logic is so simple.

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      dd/mm/yyyy is the format

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Create a new Date object for today's date, get the input and split to get the day/month/year values and then compare. I'm sure you know how to work out someone's age from two dates.

        Comment

        • Rsmastermind
          New Member
          • Sep 2008
          • 93

          #5
          Code:
          var date_of_birth=$('Id of the field where you have entered the DOB');
          var currentDate=new Date();
          var day = currentDate.getDate();
            var month = currentDate.getMonth();
            var year = currentDate.getFullYear();
            month+=1;//As month returned starts from 0 like 0 for January
           
          var Dob=date_of_birth.split('/');
          var Dob_day=Dob[0];
          var Dob_month=Dob[1];
          var Dob_year =Dob[2];
          Now you can calculate using different function I know this will be a tricky but I hope you can do because this is one of the basic programming

          Comment

          Working...