Summary of the formatting code method for Date object in JavaScript

JavaScript default time format We generally don’t use it, so you need to format it, let’s talk about the JavaScript time format method I summarized.

Many times, we can format the built-in method of Date object in JavaScript, such as:

 VAR D = New Date (); console. LOG (D); // Output: MON NOV 04 2013 21:50:33 GMT + 0800 (China Standard Time) Console.log (D. Todatestring ()); // Date String, Output: Mon Nov 04 2013 Console. LOG (D.Togmtstring ()); // Greenwich Time, Output: Mon, 04 Nov 2013 14:03:05 Gmtconsole.log (D.toisString ()); // International Standards Organization (ISO) format, output : 2013-11-04T14: 03-11-04T14: 03: 05.420zconsole.log (D.tojson ()); // Output: 2013-11-04T14: 03: 05.420zconsole.log (D.TolocaledateString ()); // Convert to Local Date format, depending on the environment, output: November 4, 2013 console.log (D.Tolocalestring ()); // Convert to local date and time format, depending on the environment, output: November 4, 2013 10:03:05console.log (D.TolocaletimeString ()); // Convert to local time format, depending on the environment, output: 10: 03: 05console.log (D. Tostring ()); // Conversion String, output: MON NOV 04 2013 22:03:05 GMT + 0800 (China Standard Time)Console.log ()); // Convert to time string, output: 22: 03: 05 GMT + 0800 (China Standard time) Console.log (D.ToutCString ()); // Convert to the world Time, Output: MON, 04 NOV 2013 14:03:05 GMT   If the above method does not meet our requirements, you can customize the function to format the time. Such as: 

// Extension to DATE, convert DATE to the String / / month of the specified format (M), day (d), hour (h), points (m), second (s), quarter (Q) can use 1-2 placeholders, // year (Y) can be used in 1-4 Placeholders, milliseconds (s) can only use one placeholder (NEW DATE ()). Format (“YYYY-MM-DD HH: mm: SS.S “) ==> 2006-07-02 08: 09: 04.423 // (New Date ()). Format (” YYYY-MD H: M: SS “) ==> 2006-7-2 8: 9: 4.18date.prototype.format = function (fmt) {// Author: meizvar o = {“m +”: this.getmonth () + 1, // Month “D +”: this.getdate (), // day “h +”: this.GetHours (), // hour “m +”: this.getminutes (), // division “s +”: this.getseconds (), // second “q +”: Math.Fllor ((this. ” GetMonth () + 3) / 3),// Quarterly “S”: this.getMilliseConds () // millisecond}; if (/((+ )/.test (FMT)) fmt = fmt.replace (regexp. $ 1, (this.GetfullyEar () + “” .substr (4 – regexp. $ 1.length); for (VAR K IN O) IF (“+ K +”) “). Test (FMT)) FMT = FMT.Replace (Regexp. $ 1 (Regexp. $ 1.length == 1)? ((“00” + O [k]). Substr (“” + O [k]). Length))))))))))))))))))

Call:

VAR TIME1 = New Date (). Format (“YYYY-MM-DD “); Var time2 = new date (). Format (” YYYY-MM-DD HH: MM: SS “);
   Method 2: 

2006-07-02 08: 09: 04.423 * (New Date ()). Pattern (“YYYY-MM-DD E HH : mm: ss “) ==> 2009-03-10 II 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-MD H: M: SS”) ==> 2006-7-2 8: 9: 4.18 * / Date.Prototype .pattern = function (fmt) {var o = {“m +”: this.getmonth () + 1, // “D +”: this.getdate (), // day “h +”: this.GetHours ()% 12 == 0? 12: this.GetHours ()% 12, // hour “h +”: this.GetHours (), // hour “m +”: this.getminutes (), // division “s +”: this. Getseconds (), // second “q +”: Math.Floor ((this.getmonth () + 3) / 3), // quarter “s”: this.getmilliseconds () // millisecond}; var week = {” 0 “:” / u65e5 “,” 1 “:” / u4e00 “,” 2 “:” / u4e09 “,” 4 “:” / u56db “,” 5 “:” / U4E94 “,” 6 “:” /u516d “};if( ((((((/ + )/.test(Fmt)}}}}}}}}} (this.getfullyear () +” “). Substr 4 – Regexp. $ 1.length;} = = /. Test (FMT)) {fmt = fmt.replace (Regexp. $ 1 ((Regexp. $ 1.1 “)? (Regexp. $ 1 .length> 2? “/ u661f / u671f”: “” “” “” + 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]): (“00” + O [k]). Substr ((“” “+ o [k]). Length))))))))) }} return fmt;} var Date = new date (); window.alert (“YYY-MM-DD HH: mm: SS”)); // ->
  
Method 3:

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 99) m = math.Round (m / 10); Return Zeroize (M); CASE ‘TT’: Return D.GETHOURS ()

Summary
  The above is the formatting code method for the Date object in JavaScript, which is given to you. I hope that everyone will help me. If you have any questions, please leave a message, the small package will reply to everyone. Thank you very much for your support of Tumi Cloud Website!                      
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment