用JavaScript(js)对时间格式化

来源:互联网 发布:sql删除dept外键约束 编辑:程序博客网 时间:2024/06/07 04:50

可以说是Web项目中不可或缺的一个Javascript类库,它可以帮助你快速的解决客户端编程的许多问题,下面贴出一个用js格式化时间的方法。

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;    }
以上代码必须先声明,然后在使用。使用方法:

var d =new Date().format('yyyy-MM-dd');

以上是转载的,源地址是:点击打开链接


0 0