Java Script Date Formatting (modified version)

Source: Internet
Author: User

Tag:for   apr    not    remove   string    efault   his   may   oct   

On the basis of previous improvements, the original code can not be run. You can use it first. 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 (/(?:d {1,4}| M{1,4}|yy (?: yy) | h{1,2}|h{1,2}|m{1,2}|s{1,2}| [llz]|tt|tt)/g, function ($) {switch ($) {case ' d ': return D.getdate (); case ' DD ': Return Zeroize (D.getdate ()); 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 [' Jan ', ' Feb ', ' Mar ', ' April ', ' may ', ' June ', ' Jul ', '][d.getmonth ', ' Sep ', ' Oct ', ' Nov ', ' Dec ' ()]; Case ' MMMM ': return [' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' September ', ' October ', ' No ' Vember ', ' 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 > a) 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]+$/); default://return quoted strings with the surrounding quotes removed return $0.substr (1, $0.length-2); } }); }; Call method: New Date (). Format ("YYYY-MM-DD HH:mm:ss L TT tt");

Java Script Date formatting (modified version)

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.