JavaScript date format (JS dates formatted)
<script type= "Text/javascript" >/* Function: Format date parameter: formatstr-format string D: Displays the day as a number without leading zeros, such as 1 DD: Displays the day as a number with a leading zero, such as the "DD" D: Displays the day as an abbreviated form, such as sun dddd: Displays the day as the full name, such as Sunday M: Displays the month as a number without leading zeros, as shown in January as 1 MM: Displays the month as a number with leading zeros, such as MMM: Displays the month as an abbreviated form, such as Jan MMMM: Displays the month For full month names, such as January yy: Displays the year in two-digit format yyyy: Year in four-digit format h: Displays the hour as a number without leading zeros using a 12-hour system Note | | Usage hh: Displays hours as a number h with leading zeros using a 12-hour system: Displays the hour as a number without a leading zero with a 24-hourly order HH: The hour is displayed as a number m with leading zeros using a 24-hour system: Displays the minutes as digits without leading zeros mm: Displays the minute as a number s with leading zeros: Displays the seconds As a number SS without leading zeros: Displays the second as a number with leading zeros L: Displays milliseconds as a number without leading zeros ll: Displays milliseconds as a digital TT with leading zeros: Display AM/PM TT: Show AM/PM return: formatted date */Date.prototype.form at = function (formatstr) {var date = this; /* Function: Fill the 0 character parameter: value-the string to fill, length-total length returned: The populated string */var zeroize = function (value, length) {if (!length) {Leng th = 2; } value = new String (value); for (var i = 0, zeros = '; i < (length-value.length); i++) {zeros + = ' 0 '; } return zeros + value; }; return Formatstr.replace (/"[^"]* "|" [^ ']* ' |\b (?:d {1,4}| M{1,4}|yy (?: yy)? | ([HHMSTT]) \1?| [LLZ]) \b/g, function ($) { Switch ($) {case ' d ': return Date.getdate (); Case ' DD ': Return Zeroize (Date.getdate ()); Case ' DDD ': return [' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thr ', ' Fri ', ' Sat '][date.getday ()]; Case ' dddd ': return [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday '][date.getday ()]; Case ' M ': Return Date.getmonth () + 1; Case ' MM ': Return Zeroize (Date.getmonth () + 1); Case ' MMM ': return [' Jan ', ' Feb ', ' Mar ', ' April ', ' may ', ' June ', ' Jul ', '][date.getmonth ', ' Sep ', ' Oct ', ' Nov ', ' Dec ' ()]; Case ' MMMM ': return [' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' September ', ' October ', ' N ' Ovember ', ' December '][date.getmonth ()]; Case ' yy ': return new String (Date.getfullyear ()). substr (2); Case ' yyyy ': return Date.getfullyear (); Case ' H ': return date.gethours ()% 12 | | 12; Case ' hh ': Return Zeroize (date.gethours ()% 12 | | 12); Case ' H ': return date.gethours (); Case ' HH ': Return Zeroize (Date.gethours ()); Case ' m ': return Date.getminutes (); Case ' mm ': Return Zeroize (Date.getminutes ()); Case ' s ': return Date.getseconds (); Case ' SS ': Return Zeroize (Date.getseconds ()); Case ' l ': return Date.getmilliseconds (); Case ' ll ': Return Zeroize (Date.getmilliseconds ()); Case ' TT ': Return date.gethours () < 12? ' Am ': ' PM '; Case ' TT ': Return date.gethours () < 12? ' AM ': ' PM '; } }); }document.writeln (New Date (). Format ("Yyyy-mm-dd hh:mm:ss")); </script>
JavaScript date format (JS dates formatted)