扩展JS Date对象时间格式化功能的小例子
更新时间:2013年12月02日 16:03:52 作者:
这篇文章主要介绍了扩展JS Date对象时间格式化功能,有需要的朋友可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
在自己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 dt = new Date();
var nowDate = dt.format("yyyy-MM-dd hh:mm:ss");
就能得到当前的时间:2013-12-02 14:02:11
相关文章
js和html5实现手机端刮刮卡抽奖效果完美兼容android/IOS
手机完美支持html5,所以如果手机端想要做个抽奖模块的话,用刮刮卡抽奖效果,相信这个互动体验是非常棒的,本人亲自完成,有错误请大家指出2013-11-11
最新评论