How to Calculate age between two dates

Discussion in 'JavaScript' started by Zack!, Dec 30, 2010.

  1. #1
    I do know there are many Javascripts available to calculate age based on current date, But I want to calculate age between two user given dates
    -
    -
    For example, a user must be able to choose the Start date,day & year from a dropdown menu.Similarly the user must also be able to choose the End date,day & year.
    -----On clicking Calculate ,the result between the dates should be displayed as age in years & months (like 24 yrs 6 months)
    -
    -
    Can anyone give me the code to set this up?
     
    Zack!, Dec 30, 2010 IP
  2. jarrodw

    jarrodw Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Check out datejs and learn to use that library. I can help you with code for a low price. PM me if you want my assistance.
     
    jarrodw, Dec 30, 2010 IP
  3. Zack!

    Zack! Active Member

    Messages:
    259
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Im not professional in writing javascripts.So dont know much about libraries & other stuff
     
    Zack!, Dec 30, 2010 IP
  4. dotcomguy

    dotcomguy Peon

    Messages:
    824
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The code below works, just change the dates to suit your calculation.

    <script type="text/javascript">

    //Set the two dates
    today=new Date()
    var christmas=new Date(today.getFullYear(), 11, 25) //Month is 0-11 in JavaScript
    if (today.getMonth()==11 && today.getDate()>25) //if Christmas has passed already
    christmas.setFullYear(christmas.getFullYear()+1) //calculate next year's Christmas
    //Set 1 day in milliseconds
    var one_day=1000*60*60*24

    //Calculate difference btw the two dates, and convert to days
    document.write(Math.ceil((christmas.getTime()-today.getTime())/(one_day))+
    " days left until Christmas!")

    </script>
     
    dotcomguy, Jan 6, 2011 IP