Date extension to JS, formatting dates

Source: Internet
Author: User

/** * Extension to Date, string months (m), days (d), 12 hours (h), 24 hours (h), minutes (m), seconds (s), Weeks (E), quarter (q) * can be used with 1-2 placeholder Emperor reign (y) with 1-4 placeholders, milliseconds (s) only can use 1 placeholders (1-3 digits) Eg: (NEW * Date ()). Format ("Yyyy-mm-dd Hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423 (NEW * date ()). Format (" Yyyy-mm-dd E HH:mm:ss ") ==> 2009-03-10 two 20:09:04 (NEW * Date  ()). Format ("Yyyy-mm-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04 (NEW * Date ()). Format ("Yyyy-mm-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04 (NEW * Date ()). Format ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18 */date.prototype.format = func tion (fmt) {var o = {"m+": this.getmonth () + 1,//Month "d+": this.getdate (),//day "H +": this.gethours ()% = = 0? 12:this . GetHours ()% 12,//hours "H +": this.gethours (),//Hour "m+": this.getminutes (),//min "s+": This.getseconds (),//sec "q+": math.fl Oor ((This.getmonth () + 3)/3),//Quarterly "S": this.getmilliseconds ()//milliseconds};var week = {"0": "/u65e5", "1": "/u4e00", "2": "/U 4e8c "," 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]): (("XX" + o[k]). substr (("" + O[k])));}} return FMT;};

  

Date extension to JS, formatting dates

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.