【IT168技术文档】
1<script> 9 var chinese = ['零','一','二','三','四','五','六','七','八','九']; 10 var len = ['十']; 11 var ydm =['年','月','日']; 12 function num2chinese(s) 13 { 14 15 //将单个数字转成中文. 16 s=""+s; 17 slen = s.length; 18 var result=""; 19 for(var i=0;i<slen;i++) 20 { 21 result+=chinese[s.charAt(i)]; 22 } 23 return result; 24 } 25 26 function n2c(s) 27 { 28 //对特殊情况进行处理. 29 s=""+s; 30 var result=""; 31 if(s.length==2) 32 { 33 if(s.charAt(0)=="1") 34 { 35 if(s.charAt(1)=="0")return len[0]; 36 return len[0]+chinese[s.charAt(1)]; 37 } 38 if(s.charAt(1)=="0")return chinese[s.charAt(0)]+len[0]; 39 return chinese[s.charAt(0)]+len[0]+chinese[s.charAt(1)]; 40 } 41 return num2chinese(s) 42 } 43 function baodate2chinese(s) 44 { 45 //验证输入的日期格式.并提取相关数字. 46 var datePat = /^(\d{2}|\d{4})(\/|-)(\d{1,2})(\2)(\d{1,2})$/; 47 var matchArray = s.match(datePat); 48 var ok=""; 49 if (matchArray == null) return false; 50 for(var i=1;i<matchArray.length;i=i+2) 51 { 52 ok+=n2c(matchArray[i]-0)+ydm[(i-1)/2]; 53 } 54 return ok; 55 } 56 </script> 57 YYYY-MM-DD:<input type=text name="mydate" value="1984-3-7"> 58 中文显示:<input type=text name="okdate"> 59 <input type=button onclick="document.all.okdate.value=baodate2chinese(document.all.mydate.value)" 60value="转换"> 61 62