Javascript日期格式化函數效能對比

來源:互聯網
上載者:User

   最近開發的軟體中需要用到日誌功能,其中有一個重要功能是顯示日期和時間。於是網上搜了一把,搜到大量的日期格式化函數,不過比較了下,感覺代碼都不夠優雅,而且效能都不給力。

  對線上一些代碼進行了評測,結果如下:

  測試代碼如下,分別對格式化函數進行50萬次計算:

代碼如下  

var start = new Date().getTime();

var date = new Date();

for(var i = 0;i<500000;i++){

date.format1('yyyy-MM-dd hh:mm:ss');

}

console.log(new Date().getTime() - start);

  函數1:

代碼如下  

// 對Date的擴充,將 Date 轉化為指定格式的String

// 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個預留位置,

// 年(y)可以用 1-4 個預留位置,毫秒(S)只能用 1 個預留位置(是 1-3 位的數字)

// 例子:

// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423

// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18

Date.prototype.format1 = 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), //季度

"S": this.getMilliseconds() //毫秒

};

if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

for (var k in o)

if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

return fmt;

}

 

  測試三次:

  成績1:6657毫秒

  成績2:6739毫秒

  成績3:6747毫秒

  平均:6714毫秒

  函數2:

代碼如下  

/** * 對Date的擴充,將 Date 轉化為指定格式的String * 月(M)、日(d)、12小時(h)、24小時(H)、分(m)、秒(s)、周(E)、季度(q)

可以用 1-2 個預留位置 * 年(y)可以用 1-4 個預留位置,毫秒(S)只能用 1 個預留位置(是 1-3 位的數字) * eg: * (new

Date()).pattern("yyyy-MM-dd hh:mm:ss.S")==> 2006-07-02 08:09:04.423

* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04

* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04

* (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04

* (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18

*/

Date.prototype.format2=function(fmt) {

var o = {

"M+" : this.getMonth()+1, //月份

"d+" : this.getDate(), //日

"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小時

"H+" : this.getHours(), //小時

"m+" : this.getMinutes(), //分

"s+" : this.getSeconds(), //秒

"q+" : Math.floor((this.getMonth()+3)/3), //季度

"S" : this.getMilliseconds() //毫秒

};

var week = {

"0" : "/u65e5",

"1" : "/u4e00",

"2" : "/u4e8c",

"3" : "/u4e09",

"4" : "/u56db",

"5" : "/u4e94",

"6" : "/u516d"

};

if(/(y+)/.test(fmt)){

fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));

}

if(/(E+)/.test(fmt)){

fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);

}

for(var k in o){

if(new RegExp("("+ k +")").test(fmt)){

fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));

}

}

return fmt;

}

 

 

  測試三次:

  成績1:7334毫秒

  成績2:7497毫秒

  成績3:7498毫秒

  平均:7443毫秒

  本著完美主義的態度,自己重新造了個更好的輪子,分享給需要的同學們,代碼如下:

代碼如下  

/**

* 對日期進行格式化,

* @param date 要格式化的日期

* @param format 進行格式化的模式字串

* 支援的模式字母有:

* y:年,

* M:年中的月份(1-12),

* d:月份中的天(1-31),

* h:小時(0-23),

* m:分(0-59),

* s:秒(0-59),

* S:毫秒(0-999),

* q:季度(1-4)

* @return String

* @author yanis.wang@gmail.com

*/

function dateFormat(date, format) {

if(format === undefined){

format = date;

date = new Date();

}

var map = {

"M": date.getMonth() + 1, //月份

"d": date.getDate(), //日

"h": date.getHours(), //小時

"m": date.getMinutes(), //分

"s": date.getSeconds(), //秒

"q": Math.floor((date.getMonth() + 3) / 3), //季度

"S": date.getMilliseconds() //毫秒

};

format = format.replace(/([yMdhmsqS])+/g, function(all, t){

var v = map[t];

if(v !== undefined){

if(all.length > 1){

v = '0' + v;

v = v.substr(v.length-2);

}

return v;

}

else if(t === 'y'){

return (date.getFullYear() + '').substr(4 - all.length);

}

return all;

});

return format;

}

 

使用方法:

dateFormat('yyyy-MM-dd hh:mm:ss');

dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss');

 

測試三次:

成績1:2903毫秒

成績2:2900毫秒

成績3:2896毫秒

平均:2899毫秒

  經過改造的函數,整體上效能提升明顯,從6714毫秒提升到2899毫秒,減少了3815毫秒,整體降到原43%的時間,效能提升一倍以上。並且從原形注入方式改為靜態函數方式,更優雅大方。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.