Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2006
    Location
    Dallas area
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    function output modification

    I'm a JS newbie so forgive me if this is a stupid question. I'm trying to use an age calculator on my site. I like the one from Javascript Kit, but I'd like it to give the age in months if the person is less than 24 months old.

    Here's the code:
    Code:
    <script>
    
    /*
    Ultimate Age calculator script- By JavaScript Kit (http://www.javascriptkit.com)
    Over 200+ free scripts here!
    Credit must stay intact for use
    */
    
    var one_day=1000*60*60*24
    var one_month=1000*60*60*24*30
    var one_year=1000*60*60*24*30*12
    
    function displayage(yr, mon, day, unit, decimal, round){
    today=new Date()
    var pastdate=new Date(yr, mon-1, day)
    
    var countunit=unit
    var decimals=decimal
    var rounding=round
    
    finalunit=(countunit=="days")? one_day : (countunit=="months")? one_month : one_year
    decimals=(decimals<=0)? 1 : decimals*10
    
    if (unit!="years"){
    if (rounding=="rounddown")
    document.write(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
    else
    document.write(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
    }
    else{
    yearspast=today.getFullYear()-yr-1
    tail=(today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()>=day)? 1 : 0
    pastdate.setFullYear(today.getFullYear())
    pastdate2=new Date(today.getFullYear()-1, mon-1, day)
    tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals
    document.write(yearspast+tail+" "+countunit)
    }
    }
    
    //Sample usage
    //displayage (year, month, day, unit, decimals, rounding)
    //Unit can be "years", "months", or "days"
    //Decimals specifies demical places to round to (ie: 2)
    //Rounding can be "roundup" or "rounddown"
    
    //displayage(1997, 11, 24, "years", 0, "rounddown")
    
    </script>
    And the call for it in the body:
    Code:
    JavaScriptKit.com is <b>
    <script>
    displayage(1997, 11, 24, "days", 0, "rounddown")
    </script>
    </b> old!<br>
    
    <b>
    <script>
    displayage(2000, 1, 1, "months", 0, "rounddown")
    </script>
    </b> has passed since the Millennium.<br>
    
    Bill Gates is
    <b>
    <script>
    displayage(1955, 10, 28, "years", 0, "rounddown")
    </script>
    </b> old.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •