直接上javaScript格式化日期的方法的源代码:
/**
* 格式化日期
*/
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');
关于javascript格式化日期,本文就介绍这么多,希望对您有所帮助,谢谢!
要饭二维码
洪哥写文章很苦逼,如果本文对您略有帮助,可以扫描下方二维码支持洪哥!金额随意,先行谢过!大家的支持是我前进的动力!

文章的版权
本文属于“洪哥笔记”原创文章,转载请注明来源地址:javascript格式化日期:http://www.splaybow.com/post/javascript-format-date.html
如果您在服务器运维、网络管理、网站或系统开发过程有需要提供收费服务,请加QQ:8771947!十年运维经验,帮您省钱、让您放心!
亲,如果有需要,先存起来,方便以后再看啊!加入收藏夹的话,按Ctrl+D!
发布时间:2014/9/1 8:55:59 | 编辑:小小菜鸟 | 分类:DHTML | 浏览: