热门关键字:
jquery > jquery教程 > javascript > javascript扩展Date的format方法

javascript扩展Date的format方法

4268
作者:管理员
发布时间:2014/11/3 18:42:34
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=458
//扩展Date的format方法   
Date.prototype.format = function (format) {  
	var o = {  
		"M+": this.getMonth() + 1,  
		"d+": this.getDate(),  
		"h+": this.getHours(),  
		"m+": this.getMinutes(),  
		"s+": this.getSeconds(),  
		"q+": Math.floor((this.getMonth() + 3) / 3),  
		"S": this.getMilliseconds()  
	}  
	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 date = new Date(parseInt("1347497754133"));
date.format("yyyy-MM-dd");




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:javascript Date format
友荐云推荐