|
// chrome 按F12,打开drawer测试 Date.prototype.format = function(fmt)
{ //author: meizz
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), //季度
};
// 年份 2015
if(/(%Y)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+""));
// 两位年份 15
if(/(%y)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(2));
//getTime返回的是以毫秒为单位的,转为秒
if(/(%s)/.test(fmt))
//fmt=fmt.replace(RegExp.$1, this.getTime()/1000);
fmt=fmt.replace(RegExp.$1, (this.getTime()+'').slice(0, 10));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (o[k].length == 2 ? o[k] : '0' + o[k]));
}
return fmt;
}
> d = new Date();
< Mon Jan 19 2015 16:54:46 GMT+0800 (中国标准时间)
> d.format('%Y-%m-%d %H:%M:%S')
< "2015-01-19 16:54:46"
> d.format('%s')
< "1421657686" |
|
|