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 |
|
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:
code as follows |
|
extension to date, converts date to a string of the specified format //month (M), day (d), hour (h), minute (m), SEC (s), quarter (q) can be used with 1-2 placeholders, //year (Y) can be used with 1-4 placeholders, and milliseconds (s) can only be used with 1 placeholders (1-3-digit digits) //Example: //(New Date ()). F Ormat ("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 P>var o = { "m+": This.getmonth () + 1,//month "d+": this.getdate (),//day "h+": this.gethours (),//hour m+: This.getminutes (),//points S+: This.getseconds (),//sec q+: Math.floor (This.getmonth () + 3)/3),//quarter "S": this.getmilliseconds ()//millisecond }; 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:
code as follows |
|
/** * Extension of date to convert date to String * Month (M), day (d), 12 hours (h), 24 hours in the specified format (H), minutes (m), SEC (s), Week (E), quarter (q) You can use 1-2 placeholder * years (y) with 1-4 placeholders, and milliseconds (s) can only be 1 placeholders (1-3 digits) * Eg: * (new Date ()). PA Ttern ("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 two 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.getdat E (),//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.getmon Th () +3)/3),//Quarter "S": this.getmilliseconds ()//milliseconds }; 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:
code as follows |
|
/** * format date, * @param date to be formatted * Pattern strings formatted @param format * 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: sec (0-59), * s: milliseconds (0-999), * Q: Quarterly (1-4) * @return String * @a Uthor 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 (), Hours "M": date.getminutes (),//min "s": Date.getseconds (),//sec "Q": Math.floor ((Date.getmonth () + 3)/ 3),//Quarter "S": date.getmilliseconds ()//milliseconds }; 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; } Use method: DateFormat (' Yyyy-mm-dd hh:mm:ss '); DateFormat (New Date (), ' yyyy-mm-dd hh:mm:ss '); Test three times: Score 1:2,903 milliseconds Score 2:2,900 milliseconds Score 3:2,896 milliseconds Average: 2899 milliseconds |
Modified functions, overall performance improvement, from 6714 milliseconds to 2899 milliseconds, reduced by 3815 milliseconds, overall down 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.