JavaScript date format (JS dates formatted)

Source: Internet
Author: User
Tags getdate

Method One: This is very good, seems to be written by Csdn's Meizz:

[JavaScript] View Plain copy//  the extension to date, converting  Date  to the string   //  month (m), Day (d), hour (h), Minute (m), Seconds (s), quarter (q)   can use  1-2  placeholders,   //  years (y) can be used with  1-4  placeholders, milliseconds (s) can only be used  1   Placeholder (is a number of  1-3  digits)    //  example:   //  (New date ()). Format ("Yyyy-mm-dd hh:mm:ss. S ")  ==> 2006-07-02 08:09:04.423   //  (New date ()). Format ("YYYY-M-D H:M:S.S")       ==> 2006-7-2 8:9:4.18     date.prototype.format = function (FMT)     { //author: meizz       var o = {         "m+"  :  This.getmonth () +1,                  //month          "d+"  : this.getdate (),                    //Day          "h+"  : this.gethours (),                    //hours          "m+"  : this.getminutes (),                  //         "s+" &NBSP:  this.getseconds (),                  //sec          "q+"  : math.floor ((This.getmonth () +3) /3),  //quarterly          "S"   : this.getmilliseconds ()               //milliseconds       };      &nbsP;if (/(y+)/.test (FMT))         fmt=fmt.replace (regexp.$1,  ( This.getfullyear () + ""). substr (4 - regexp.$1.length));      for (var k  in o)         if (New regexp ("(" + k +) "). Test (FMT))       fmt = fmt.replace (regexp.$1,  (regexp.$1.length==1)  ?  (o [K])  :  (("+ o[k]). substr ((" "" + o[k]) (length));      return fmt ;   }  

Call Method:

[JavaScript] view plaincopy var time1 = new Date (). Format ("Yyyy-mm-dd HH:mm:ss");    var time2 = new Date (). Format ("Yyyy-mm-dd");
Method Two:[JavaScript] View plain copy <mce:script language= "javascript"  type= "Text/javascript" ><!--           /**         *  An extension of date that converts  Date  into a string         *  month (M), day (d), 12-hour ( h), 24 hours (h), minutes (m), SEC (s), Week (E), quarter (q)   can be used  1-2  placeholders          *   year (y) can be used  1-4  placeholders, milliseconds (s) can only be used  1  placeholder (is the number of  1-3  digits)           * eg:         *  (New date ()). Pattern (" Yyyy-mm-dd hh:mm:ss. S ")  ==> 2006-07-02 08:09:04.423         *  (new  date ()). Pattern ("Yyyy-mm-dd e hh:mm:ss")  ==> 2009-03-10  two  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-m-d &NBSP;H:M:S.S ")  ==> 2006-7-2 8:9:4.18         */            date.prototype.pattern=function (FMT)  {                 var o = {                  "m+"  :  This.getmonth () +1, //month                   "d+"  : this.getdate (),  //day              &NBSP;&NBSP;&Nbsp;  "h+"  : this.gethours ()%12 == 0 ? 12 : this.gethours ()%12,  //hours                  "h+"   :  this.gethours (),  //hours                   "m+"  : this.getminutes (),  //min                   "s+"  : this.getseconds (),  //sec                   "q+"  : math.floor (This.getmonth () +3)/3),  //quarterly                  "S"  : this.getmilliseconds ()  //Ms                  };                 var week = {                  "0"  :  "/u65e5",                  "1"  :  "/u4e00",                  "2"  :  "/u4e8c",                 "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])  :  (("+ o[k])" Substr (("" + o[k). Length));                     }                 }                 return fmt;            }                   var date = new date ();          Window.alert (dAte.pattern ("Yyyy-mm-dd hh:mm:ss"));  // --></mce:script>      

Method Three:

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 ($)  {                     &Nbsp;    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 [', ' Feb ', ' Mar ', ' Apr ', ' may ', ' June ', ' June ', ' Aug ', ' Sep ', ' Oct ', ' Nov ', ' Dec '][d.getmonth ()];                              case   ' MMMM ':     return [' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' September ', ' 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  | | &NBSP;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 =&n

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.