<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>

解决方案 »

  1.   

    emu老大,具体把这里的年月时间再改变下,会少用些对象呀
    为什么不直接在Date对象上扩充呀
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function Date.prototype.toString(){
    return this.getFullYear()+"年"+(this.getMonth()+1)+"月"+this.getDate()+"日";
    }
    var d = new Date();
    alert(d);
    //-->
    </SCRIPT>
      

  2.   

    把emu兄弟的代码改成这样,就可以去掉那个函数调用
    <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>
      

  3.   

    String.prototype.toCHS=function()

        return this.replace(/\d/g, 
            function(a){return "零一二三四五六七八九".charAt(parseInt(a))}); 
    }这样写不是更爽??
      

  4.   

    meizz强!
    说实在的,我一直以为replace过程是无法调用函数的。怎么突然可以了呢!?
      

  5.   

    晕,找了一下资料,原来这样也行
    <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>
      

  6.   

    抛弃了原型方法,把代码调整了一下,这下面目全非了,嘿嘿。function toCHS(s){return s.constructor!=Number?s.constructor!=String?s.constructor!=Date?null:toCHS(s.getFullYear()+"")+"年"+toCHS(s.getMonth()+1)+"月"+toCHS(s.getDate())+"日"+toCHS(s.getHours())+"时"+toCHS(s.getMinutes())+"分"+toCHS(s.getSeconds())+"秒": s.replace(/\d/g,function(a){return"零一二三四五六七八九".charAt(parseInt(a))}): toCHS(((s>19?Math.floor(s/10):"")+(s>9?("十"):"")+(s%10==0&&s>0?"":s%10)))}
    alert(toCHS(new Date()));