`
frank1998819
  • 浏览: 733445 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类

js javascript 格式化 日期 时间(转)

 
阅读更多
/**
* 时间对象的格式化
*/
Date.prototype.format = function(format)
{
/*
* format="yyyy-MM-dd hh:mm:ss";
*/
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 time = new Date();
time.format("MM-dd hh:mm");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics