Thread: Age Calculation
View Single Post
  #31  
Old 05-08-2010, 04:24 PM
 
Shamun Shamun is offline
 

X-Adept
  
Join Date: Jun 2009
Location: North Carolina
Posts: 841
 

Default Re: Age Calculation

Code:
{* For http://forum.x-cart.com/showthread.php?t=53578 Call extra fields via this: {$extra_fields.0.field_value} {$extra_fields.1.field_value} {$extra_fields.2.field_value} *} <script type="text/javascript"> var startyear = "1950"; var endyear = "2010"; var dat = new Date(); var curday = dat.getDate(); var curmon = dat.getMonth()+1; var curyear = dat.getFullYear(); var calday = {$extra_fields.1.field_value}; var calmon = {$extra_fields.0.field_value}; var calyear = {$extra_fields.2.field_value}; {literal} function checkleapyear(datea) { if(datea.getYear()%4 == 0) { if(datea.getYear()% 10 != 0) { return true; } else { if(datea.getYear()% 400 == 0) return true; else return false; } } return false; } function DaysInMonth(Y, M) { with (new Date(Y, M, 1, 12)) { setDate(0); return getDate(); } } function datediff(date1, date2) { var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(), y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate(); if (d1 < d2) { m1--; d1 += DaysInMonth(y2, m2); } if (m1 < m2) { y1--; m1 += 12; } return [y1 - y2, m1 - m2, d1 - d2]; } function calage() { var curd = new Date(curyear,curmon-1,curday); var cald = new Date(calyear,calmon-1,calday); var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0); var dife = datediff(curd,cald); {/literal} document.getElementById('dAge').innerHTML = dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days"; {literal} } $(document).ready(function(){ calage(); }); {/literal} </script> He is <span id="dAge">5 months</span> old



There you go. There is no forms in that and it's text based.
You can customize what it says at the bottom. Anything between the <span></span> tags are from the script so you can change anything outside of that. You'll probably need to change the order for which the age is though. At the top with the smarty variables, I have it as 1 0 2.

If that works, you'll want to put the script in an actual external .js file so the browser can cache it. Faster load times and less bandwith. The " He is <span id="dAge">5 months</span> old" shouldn't be in a .js file though.
__________________
- Shane Munroe
Reply With Quote