<SCRIPT LANGUAGE="JavaScript">
<!--
String.prototype.toCHS=function(){
return this.replace(/1/g,"一").replace(/2/g,"二").replace(/3/g,"三").replace(/4/g,"四").replace(/5/g,"五").replace(/6/g,"六").replace(/7/g,"七").replace(/8/g,"八").replace(/9/g,"九").replace(/0/g,"零");
}
Number.prototype.toCHS=function(){
return ((this>19?Math.floor(this/10):"")+(this>9?("十"):"")+(this%10==0&&this>0?"":this%10)).toCHS();
}
function D(x){
var a=x.getFullYear().toString().toCHS(),b=(x.getMonth()+1).toCHS(),c=x.getDate().toCHS(),d=x.getHours().toCHS(),e=x.getMinutes().toCHS(),f=x.getSeconds().toCHS();
return a+"年"+b+"月"+c+"日"+d+"时"+e+"分"+f+"秒";
}
alert(D(new Date()));
//-->
</SCRIPT>
<!--
String.prototype.toCHS=function(){
return this.replace(/1/g,"一").replace(/2/g,"二").replace(/3/g,"三").replace(/4/g,"四").replace(/5/g,"五").replace(/6/g,"六").replace(/7/g,"七").replace(/8/g,"八").replace(/9/g,"九").replace(/0/g,"零");
}
Number.prototype.toCHS=function(){
return ((this>19?Math.floor(this/10):"")+(this>9?("十"):"")+(this%10==0&&this>0?"":this%10)).toCHS();
}
function D(x){
var a=x.getFullYear().toString().toCHS(),b=(x.getMonth()+1).toCHS(),c=x.getDate().toCHS(),d=x.getHours().toCHS(),e=x.getMinutes().toCHS(),f=x.getSeconds().toCHS();
return a+"年"+b+"月"+c+"日"+d+"时"+e+"分"+f+"秒";
}
alert(D(new Date()));
//-->
</SCRIPT>
为什么不直接在Date对象上扩充呀
<SCRIPT LANGUAGE="JavaScript">
<!--
function Date.prototype.toString(){
return this.getFullYear()+"年"+(this.getMonth()+1)+"月"+this.getDate()+"日";
}
var d = new Date();
alert(d);
//-->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!--
String.prototype.toCHS=function(){
return this.replace(/1/g,"一").replace(/2/g,"二").replace(/3/g,"三").replace(/4/g,"四").replace(/5/g,"五").replace(/6/g,"六").replace(/7/g,"七").replace(/8/g,"八").replace(/9/g,"九").replace(/0/g,"零");
}
Number.prototype.toCHS=function(){
return ((this>19?Math.floor(this/10):"")+(this>9?("十"):"")+(this%10==0&&this>0?"":this%10)).toCHS();
}
Date.prototype.toString=function(){
return this.getFullYear().toString().toCHS()+"年"+(this.getMonth()+1).toCHS()+"月"+this.getDate().toCHS()+"日"+this.getHours().toCHS()+"时"+this.getMinutes().toCHS()+"分"+this.getSeconds().toCHS()+"秒";
}
var d = new Date();
alert(d);//-->
</SCRIPT>
{
return this.replace(/\d/g,
function(a){return "零一二三四五六七八九".charAt(parseInt(a))});
}这样写不是更爽??
说实在的,我一直以为replace过程是无法调用函数的。怎么突然可以了呢!?
<script language=javascript>
String.prototype.toCHS=function()
{
return this.replace(/(\d)(\d)/g,
function($0,$1,$2){
return ("零一二三四五六七八九".charAt(parseInt($1)))+"十"+("零一二三四五六七八九".charAt(parseInt($2)));
});
}
alert("22".toCHS())
</SCRIPT>
alert(toCHS(new Date()));