<div class="codetitle"> <span><a style="CURSOR: pointer" data="54784" class="copybut" id="copybut54784" onclick="doCopy('code54784')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code54784"> <br><script language="javascript"> <BR> function ages(str) <BR> { <BR> var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); <BR> if(r==null)return false; <BR> var d= new Date(r[1], r[3]-1, r[4]); <BR> if (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]) <BR> { <BR> var Y = new Date().getFullYear(); <BR> return("年龄 = "+ (Y-r[1]) +" 周岁"); <BR> } <BR> return("输入的日期格式错误!"); <BR> } <BR> alert(ages("1980-03-22")); <BR> alert(ages("2002-01-31")); <BR> alert(ages("2002-01-41")); <BR> </script><br> </div>