Hello,
Is it possible to enter a date of birth and have the wiki calculate an age from this?
Something like [AGE]28/7/2005[/AGE] could render as "5 years, 4 days old"
Hello,
Is it possible to enter a date of birth and have the wiki calculate an age from this?
Something like [AGE]28/7/2005[/AGE] could render as "5 years, 4 days old"
There is no such mechanism. If there is a BB-Code mod that does, it should still work in the wiki.
- lead developer for VaultWiki
Worked it out myself as a BBCode replacement!
Title: Age
BB Code Tag Name: Age
Replacement
Example: [age]19760925[/age]Code:<script type="text/javascript"> var now = new Date(); var today = new Date(now.getYear(),now.getMonth(),now.getDate()); var yearNow = now.getYear(); var monthNow = now.getMonth(); var dateNow = now.getDate(); var datestring = {param} + ''; var dob = new Date(datestring.substring(0,4), datestring.substring(4,6)-1, datestring.substring(6,8)); var yearDob = dob.getYear(); yearAge = yearNow - yearDob; var monthDob = dob.getMonth(); if (monthNow >= monthDob) var monthAge = monthNow - monthDob; else { yearAge--; var monthAge = 12 + monthNow -monthDob; }; if (monthAge > 1) var monthDisplay = ' Months Old'; else { var monthDisplay = ' Month Old' }; document.write (yearAge + ' Years, ' + monthAge + monthDisplay); </script>
Description: The date of birth in the format YYYYMMDD
Use {option}: No
Icon: age.gif
Edit: looks like there is a minor bug - after you first submit a post with this code if messes up the AJAX load, you need to refresh the page
Last edited by Moses; October 26, 2010 at 11:04 PM.
document.write will erase the whole page if it is run on an AJAX load. Instead you should create a function to do this on the server-side, or use Javascript like this:
Note that while this will not erase the page if submitted via AJAX, the span won't show the relative timestamp after AJAX. If you need to satisfy all this, you'll need to parse and output the timestamp in PHP by using a callback function for your BB-Code.Code:<script type="text/javascript"> <!-- if (typeof('birthday_bbcode') == "undefined") { var birthday_bbcode = true; YAHOO.util.Event.onDOMReady(function() { var datestring, dob, yearDob, monthDob, yearAge, monthAge, monthDisplay; var now = new Date(); var today = new Date(now.getYear(),now.getMonth(),now.getDate()); var yearNow = now.getYear(); var monthNow = now.getMonth(); var dateNow = now.getDate(); var birthdays = YAHOO.util.Dom.getElementsByClassName("birthday", "span"); for (var i = 0; i < birthdays.length; i++) { datestring = birthdays[i].innerHTML; dob = new Date( datestring.substring(0,4), datestring.substring(4,6)-1, datestring.substring(6,8) ); yearDob = dob.getYear(); yearAge = yearNow - yearDob; monthDob = dob.getMonth(); if (monthNow >= monthDob) monthAge = monthNow - monthDob; else { yearAge--; monthAge = 12 + monthNow -monthDob; } if (monthAge > 1) monthDisplay = ' Months Old'; else { monthDisplay = ' Month Old' } birthdays[i].innerHTML = yearAge + ' Years, ' + monthAge + monthDisplay; } }); } //--> </script> <span class="birthday">{param}</span>
- lead developer for VaultWiki
Bookmarks