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.
Did you think to use Google? calculate age in javascript for client side or calculate age in php for server side. Do some study on your own. cheers, gary
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:
currentAge = birthYear - currentYear Use PHP , not JS . If you page needs to be pure HTML then call the PHP via AJAX.
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! 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.
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.
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 - 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.
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!
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
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
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
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.
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.
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?
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.
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.