JavaScript Date Format Function performance comparison

Source: Internet
Author: User
Tags dateformat getdate

Log functionality is needed in recently developed software, and one of the important features is the display of dates and times. So a search on the Internet, search to a large number of date format functions, but compared to the next, the sense of the code is not elegant, and performance is not to force.

Some of the code on the line was evaluated and the results were as follows:

The test code is as follows, and the Format function is evaluated 500,000 times, respectively:

The code is as follows Copy Code

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);


Function 1:

The code is as follows Copy Code

An extension to date that converts date to a string of the specified format

Months (m), days (d), hours (h), minutes (m), seconds (s), quarter (q) can be used with 1-2 placeholders,

Year (y) you can use 1-4 placeholders, milliseconds (s) to use only 1 placeholders (1-3-digit digits)

Example:

(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,//month

"D+": this.getdate (),//day

"h+": this.gethours (),//hour

"m+": this.getminutes (),//min

"S+": This.getseconds (),//sec

"q+": Math.floor (This.getmonth () + 3)/3),//Quarter

"S": this.getmilliseconds ()//MS

};

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]): (("+ o[k]"). substr (("" + o[k). length));

return FMT;

}

Tested three times:

Score 1:6,657 milliseconds

Score 2:6,739 milliseconds

Score 3:6,747 milliseconds

Average: 6714 ms

Function 2:

The code is as follows Copy Code

/** * Extension of date to convert date to String * month (m), Day (d), 12 hours (h), 24 hours (h), minutes (M), seconds (s), Week (E), quarter (q) in the specified format

can use 1-2 placeholder * year (y) can use 1-4 placeholders, milliseconds (S) can only use 1 placeholders (1-3 digits) * 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 II 20:09:04

* (New Date ()). Pattern ("Yyyy-mm-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04

* (New Date ()). Pattern ("Yyyy-mm-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 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,//month

"D+": this.getdate (),//day

"h+": this.gethours ()%12 = 0? 12:this.gethours ()%12,//hour

"h+": this.gethours (),//hour

"m+": this.getminutes (),//min

"S+": This.getseconds (),//sec

"q+": Math.floor (This.getmonth () +3)/3),//Quarter

"S": this.getmilliseconds ()//MS

};

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]): (("+ o[k]"). substr (("" + o[k). length));

}

}

return FMT;

}


Tested three times:

Score 1:7,334 milliseconds

Score 2:7,497 milliseconds

Score 3:7,498 milliseconds

Average: 7443 ms

In the spirit of perfectionism, I have built a better wheel, sharing the necessary students, the code is as follows:

The code is as follows Copy Code

/**

* The date is formatted,

* @param date on which date is to be formatted

* Pattern string Formatted @param format

* The supported pattern letters are:

* Y: Year,

* M: Months of the Year (1-12),

* D: Days of the Month (1-31),

* H: Hours (0-23),

* M: Min (0-59),

* S: Seconds (0-59),

* S: Millisecond (0-999),

* Q: Quarterly (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,//month

"D": date.getdate (),//day

"H": date.gethours (),//hour

"M": date.getminutes (),//min

"S": Date.getseconds (),//sec

"Q": Math.floor (Date.getmonth () + 3)/3),//Quarter

"S": date.getmilliseconds ()//MS

};

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;

}

How to use:

DateFormat (' Yyyy-mm-dd hh:mm:ss ');

DateFormat (New Date (), ' yyyy-mm-dd hh:mm:ss ');

Tested three times:

Score 1:2,903 milliseconds

Score 2:2,900 milliseconds

Score 3:2,896 milliseconds

Average: 2899 ms

The modified function increases the overall performance significantly, from 6714 milliseconds to 2899 milliseconds, reduced by 3815 milliseconds, overall to the original 43% time, performance increased by more than one times. And from the prototype injection mode to static function, more elegant and generous.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.