Mon 28 Apr 2008

javascript对时间格式化

JavaScript通过原型函数的封装对時間格式化函數:

<script>
    Date.prototype.format = function(format)
    {
        var o = {
            "M+" : this.getMonth()+1, //month
            "d+" : this.getDate(),    //day
            "h+" : this.getHours(),   //hour
            "m+" : this.getMinutes(), //minute
            "s+" : this.getSeconds(), //second
            "q+" : Math.floor((this.getMonth()+3)/3), //quarter
            "S" : this.getMilliseconds() //millisecond
        }
        if(/(y+)/.test(format)) {
            format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }

        for(var k in o) {
            if(new RegExp("("+ k +")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
            }
        }
        return format;
    }

    showtime = new Date().format("yyyy-MM-dd hh:mm:ss");

    alert(showtime);
</script>

参考: http://www.javaeye.com/topic/183162

链接:

Tags: web, javascript

进来Spamer太多,暂时关闭留言,如果有什么问题可以邮件联系。
borderj # gmail.com (Gtalk)
http://www.b0rder.com