1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Perpetually updated birthdate for bio page

Discussion in 'JavaScript' started by solid7, Oct 20, 2013.

  1. #1
    Can someone please tell me the easiest way to write a short bit of code to keep ages in a biographical page constantly updated? In other words, if I have a birthday of Jan 1 2000, I want the displayed age to be kept up to date, based on that constant.

    Thank you.
     
    solid7, Oct 20, 2013 IP
  2. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #2
    kk5st, Oct 21, 2013 IP
  3. Ricardo Neves

    Ricardo Neves Greenhorn

    Messages:
    39
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    20
    #3
    Hope this helps.

    <html>
    <head>...</head>
    <body>
    I am <span id="age">20</span> years old
    
    <script type="text/javascript">
    
        var birthMonth=0, birthDay=10, birthYear=1992
        todayDate = new Date();
        todayYear = todayDate.getFullYear();
        todayMonth = todayDate.getMonth();
        todayDay = todayDate.getDate();
        age = todayYear - birthYear;
        if (todayMonth <= birthMonth)
        {       
            if(todayMonth == birthMonth && todayDay<birthDay)
                age--;
            else if(todayMonth < birthMonth)
                age--;
        }
           
        document.getElementById('age').innerHTML = age;
    
    </script>
    </body>
    </html>
    HTML:
     
    Ricardo Neves, Oct 21, 2013 IP
  4. ApocalypseXL

    ApocalypseXL Notable Member

    Messages:
    6,095
    Likes Received:
    103
    Best Answers:
    5
    Trophy Points:
    240
    #4
    currentAge = birthYear - currentYear

    Use PHP , not JS . If you page needs to be pure HTML then call the PHP via AJAX.
     
    ApocalypseXL, Oct 21, 2013 IP
  5. solid7

    solid7 Well-Known Member

    Messages:
    459
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    148
    #5
    No, your sageliness. I didn't think of that. I felt like asking the answer straight up, so somebody could give me a wise-ass reply like that one. Great job! :p

    I don't have the ability to use PHP in the page I am using this in. I can, however, JS.

    Thank you for your reply.


    Is it possible to correct the date input by adding a '+1', so that the date variable is input by the traditional format?

    I will be using this alot!

    Thanks for your help.
     
    Last edited by a moderator: Oct 21, 2013
    solid7, Oct 21, 2013 IP
  6. Ricardo Neves

    Ricardo Neves Greenhorn

    Messages:
    39
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    20
    #6
    I don't understand your question. traditional format? Can you give some examples?
     
    Ricardo Neves, Oct 21, 2013 IP
  7. solid7

    solid7 Well-Known Member

    Messages:
    459
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    148
    #7

    This isn't working for me, as written. I have assumed that you meant to end the first line of variables. I tried adding the semicolon, but that does not produce a result.

    As in, Jan = 1, rather than Jan =0 in the date format.
     
    Last edited by a moderator: Oct 21, 2013
    solid7, Oct 21, 2013 IP
  8. Ricardo Neves

    Ricardo Neves Greenhorn

    Messages:
    39
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    20
    #8
    The "jan=0" is just for code. Don't you want to show your age always updated on a page?
    Just put
    <span id="age"></span>
    Code (markup):
    instead of your age on html.

    Then you put this on the page:
    <script type="text/javascript">
    
    var birthMonth=0, birthDay=10, birthYear=1992
    todayDate = new Date();
    todayYear = todayDate.getFullYear();
    todayMonth = todayDate.getMonth();
    todayDay = todayDate.getDate();
    age = todayYear - birthYear;
    if (todayMonth <= birthMonth)
    {
    if(todayMonth == birthMonth && todayDay<birthDay)
    age--;
    else if(todayMonth < birthMonth)
    age--;
    }
    
    document.getElementById('age').innerHTML = age;
    
    </script>
    Code (markup):
    And replace
    var birthMonth=0, birthDay=10, birthYear=1992
    Code (markup):
    for your date of birth. birthmonth is your birth month -1.
    If you want to use the traditional format use this instead:

    <script type="text/javascript">
    
    var birthMonth=1, birthDay=10, birthYear=1992;
    birthMonth = (birthMonth-1);
    todayDate = new Date();
    todayYear = todayDate.getFullYear();
    todayMonth = todayDate.getMonth();
    todayDay = todayDate.getDate();
    age = todayYear - birthYear;
    if (todayMonth <= birthMonth)
    {
    if(todayMonth == birthMonth && todayDay<birthDay)
    age--;
    else if(todayMonth < birthMonth)
    age--;
    }
    
    document.getElementById('age').innerHTML = age;
    
    </script>
    Code (markup):
     
    Ricardo Neves, Oct 21, 2013 IP
    solid7 likes this.
  9. solid7

    solid7 Well-Known Member

    Messages:
    459
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    148
    #9
    Ricardo Neves - I am beginning to think that there may be some other issue at work here. I have tried your code, but it does not work. Am wondering if maybe there is an issue with the host filtering javascript. I can do simple things. For instance, I ran a couple of test routines, (simple alert boxes) and they worked fine. However, your code doesn't work, no matter what I do. I do believe that it worked for you, hence the fact that you posted it.

    I initially posted this question, as I tried several other scripts that I found online, and modified them to be inline, rather than sever side executable. Nothing worked. I even tried removing all of the error handling, just to simplify the code, and try to obtain some sort of result. Nothing.

    Just wondering if you might humor me, and set up a test page, which I can open the page source? Just to make sure that I am not missing something else...

    Thank you so much for your help.
     
    solid7, Oct 21, 2013 IP
  10. Ricardo Neves

    Ricardo Neves Greenhorn

    Messages:
    39
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    20
    #10
    No problem, here you go.
    (Check the attachments)
     

    Attached Files:

    Ricardo Neves, Oct 21, 2013 IP
    solid7 likes this.
  11. solid7

    solid7 Well-Known Member

    Messages:
    459
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    148
    #11
    Thank you... I have figured out that the script was OK, all along. It's my webpage. There are scripts that are filtered administratively. I didn't know.

    Apparently, there is a workaround, so I'm looking into that.

    Thank you again for your help!
     
    solid7, Oct 21, 2013 IP
  12. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #12
    What an ignorant statement. A simple search would have given you excellent answers immediately. Did you even use the links I provided? Obviously not, because you're still here receiving less than complete or less than correct "solutions" that you don't know how to apply.

    I'm sure you're all too knowledgeable to need to read How To Ask Questions The Smart Way, but on the off chance you're more willing to learn than evidence suggests, it provides clue that you're lacking.

    g
     
    kk5st, Oct 21, 2013 IP
  13. solid7

    solid7 Well-Known Member

    Messages:
    459
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    148
    #13
    I searched alot of links before I posted, but thank you for making assumptions.

    Etiquette is a 2-way street. There's an old saying about a contentious mouth calling for strokes... I'm sure you're smart enough to get the picture. But I've had enough squabbling with you. Try being nice next time. It's contagious :)
     
    solid7, Oct 21, 2013 IP
  14. Ricardo Neves

    Ricardo Neves Greenhorn

    Messages:
    39
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    20
    #14
    No problem. ;)
     
    Ricardo Neves, Oct 21, 2013 IP
  15. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #15
    I think that word doesn't mean what you think it means.

    etiquette
    n 1: rules governing socially acceptable behavior
    STFW and RTFM are perfectly appropriate net etiquette of long standing.

    you failed to exhibit socially acceptable behavior by 1) failing to search the web or 2) by failing to explain why what you found didn't do the job.
    I didn't make assumptions, you did when you assumed we would just know that you had made this basic bit of research. Don't project your failings on me.

    Wow! This is your idea of polite discourse?

    g
     
    kk5st, Oct 21, 2013 IP
  16. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #16
    Is your page a self hosted one or are you working on someone else's system?

    When debugging javascript it's great to use a browser that isn't IE because of the inspect and console tools that are provided. They're all slightly different so play around and find the one you are most comfortable with.
     
    sarahk, Oct 21, 2013 IP
    Arick unirow likes this.
  17. solid7

    solid7 Well-Known Member

    Messages:
    459
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    148
    #17
    Whatever, man. Let it go. You're passive aggressive, and I can accept that.

    THE END.

    I am working on someone else's system, and I clearly don't have a complete understanding yet of what I can and cannot do on it.

    I would never use IE, for anything other than compatibility testing, and ONLY then, after I've made it work right in everything else. Currently, I use Firefox and Safari. I like the developer tools in Firefox, and I also use JSLint as a debugger.
     
    Last edited by a moderator: Oct 21, 2013
    solid7, Oct 21, 2013 IP
  18. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #18
    So when you use Firefox it should give you some feedback on the javascript outcomes and you can put console.log() commands in to help with the debugging. Have you tried that?
     
    sarahk, Oct 21, 2013 IP
  19. solid7

    solid7 Well-Known Member

    Messages:
    459
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    148
    #19
    Yep. That's why I was saying that I believed that the code worked for the author... Because it wasn't returning errors. It just wasn't being allowed to execute! I am almost certain that there is an error caused by another set of code in the webpage or control panel.
     
    solid7, Oct 21, 2013 IP
  20. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #20
    neves' example is decent, but I'd suggest flipping the logic thus:
    function getAge(d,m,y) {
    
    	var
    		now = new Date(),
    		nowMonth = now.getMonth() + 1,
    		age = now.getFullYear() - y;
    		
    	if (age < 0) return false;
    		
    	if (
    		(nowMonth > m) ||
    		((nowMonth == m) && (now.getDate() >= d))
    	) age++;
    	
    	return age;
    		
    }
    Code (markup):
    A bit simpler and faster.

    -- edit -- I too agree this SHOULD probably be handled server-side, not client side. If PHP isn't available I'd be looking for whatever it is the site is done in... unless you're gonna show it in realtime (unlikely since only checking year) this REALLY doesn't belong in scripting... unless of course you literally have no access to the server side code building the page. (which sounds like your situation) in which case I'd suggest moving the site to someplace you can do it.
     
    Last edited: Oct 23, 2013
    deathshadow, Oct 23, 2013 IP
    solid7 likes this.