Date.prototype.format_javascript techniques for TIME format processing functions under JavaScript

Source: Internet
Author: User
Tags getdate

Example one:

A full JS time processing function, although I did not carefully to study inside the regular, but I passed the test, is very good, you can according to your own needs to set the time format of the string output, I applied to the format: mm/dd/yyyy hh:mm tt and YYYY-MM-DD HH:mm:ss.

<span style= "FONT-SIZE:18PX;" 
>date.prototype.format = function (mask) {var d = this; 
var zeroize = function (value, length) {if (!length) length = 2; 
Value = String (value); 
for (var i = 0, zeros = '; i < (length-value.length); i++) {zeros = = ' 0 '; 
return zeros + value; 
}; return Mask.replace (/"[^"]* "|") [^ ']* ' |\b (?:d {1,4}|m{1,4}|yy (?: yy)? | ([HHMSTT]) \1?| [Llz]) 
\b/g, function ($) {switch ($) {case ' d ': return D.getdate (); 
Case ' DD ': Return Zeroize (D.getdate ()); 
Case ' DDD ': return [' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thr ', ' Fri ', ' Sat '][d.getday ()]; 
Case ' dddd ': return [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday '][d.getday ()]; 
Case ' M ': Return D.getmonth () + 1; 
Case ' MM ': Return Zeroize (D.getmonth () + 1); 
Case ' MMM ': return [' Out of the window ', ' Feb ', ' Mar ', ' Apr ', ' may ', ' June ', ' o ', ' Aug ', ' Sep ', ' Oct ', ' Nov ', ' Dec '][d.getmonth ()]; Case ' MMMM ': return [' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' Septe 'Mber ', ' October ', ' November ', ' December '][d.getmonth ()]; 
Case ' yy ': Return String (D.getfullyear ()). substr (2); 
Case ' yyyy ': return D.getfullyear (); Case ' H ': return d.gethours ()% 12 | | 
12; 
Case ' hh ': Return Zeroize (d.gethours ()% 12 | | 12); 
Case ' H ': return d.gethours (); 
Case ' HH ': Return Zeroize (D.gethours ()); 
Case ' m ': return D.getminutes (); 
Case ' mm ': Return Zeroize (D.getminutes ()); 
Case ' s ': return D.getseconds (); 
Case ' SS ': Return Zeroize (D.getseconds ()); 
Case ' l ': Return Zeroize (D.getmilliseconds (), 3); 
Case ' L ': var m = d.getmilliseconds (); 
if (M >) m = Math.Round (M/10); 
Return Zeroize (m); Case ' TT ': Return d.gethours () < 12? 
' Am ': ' PM '; Case ' TT ': Return d.gethours () < 12? 
' AM ': ' PM '; 
Case ' Z ': Return d.toutcstring (). Match (/[a-z]+$/); 
Return quoted strings with the surrounding quotes removed Default:return $0.substr (1, $0.length-2); 
} 
});  };</span>

Example two:

The Date.prototype.format method extends the format method in the prototype of date so that it can easily format the date format output.

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]:
("+ o[k]"). substr (("" "+ o[k]). length);
return format;
}
var date = new Date (parseint ("1347497754133"));
Date.format ("Yyyy-mm-dd");

The above through two pieces of instance code to introduce the JavaScript time format processing function Date.prototype.format related content, I hope to help.

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.