JavaScript Date formatting method Rollup _javascript Tips

Source: Internet
Author: User
Tags getdate

Method One:

An extension to date that converts date to a string
/month (M) in the specified format, days (d), hours (h), minutes (M), seconds (s), quarter (q) can be used in 1-2 placeholders, 
//year (Y) can be 1-4 placeholders, milliseconds (s) can only be 1 Placeholder (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 (),//hour 
    " m+ ": this.getminutes (),// 
    " s+ ": This.getseconds (),//sec 
    " q+ ": Math.floor ((This.getmonth () + 3)/3),//Quarter 
    "S": this.getmilliseconds ()//Millisecond 
  };
  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 Mode:

var time1 = new Date (). Format ("Yyyy-mm-dd");
var time2 = new Date (). Format ("Yyyy-mm-dd HH:mm:ss");

Method Two:

<script language= "javascript" type= "Text/javascript" > <!--/** * The extension of date to the specified format of String * Month (M), day (d), 12 small (h), 24 hours (h), minutes (m), SEC (s), Week (E), quarter (q) can use 1-2 placeholder * year (y) can use 1-4 placeholders, milliseconds (s) can only use 1 placeholders (1-3 digits) * Eg: * (new Date ()). Pat Tern ("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 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-m-d 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 "h+": this.gethours ()%12 = = 0? 12:this.gethours ()%12,//hr "h+": this.gethours (),//Hour "m+": this.getminutes (),//"s+": this.   Getseconds (),//sec "q+": Math.floor (This.getmonth () +3)/3),//Quarter  
  "S": this.getmilliseconds ()//millisecond}; var week = {"0": "/u65e5", "1": "/u4e00", "2": "/u4e8c", "3": "/u4e09", "4": "/u5     
  6db "," 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>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"));
 --> </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 ($) {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 [' Out of the ', ' Feb ', ' Mar ', ' Apr ', ' may ', ' June ', ' may ', ' 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 | | 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 >) 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]+$/);
    Return quoted strings with the surrounding quotes removed Default:return $0.substr (1, $0.length-2);
}
  });

 };

Method Four:

<script type= "Text/javascript" >
var date = new Date ();
Document.writeln (date);
Thu the 2015 01:25:53 gmt+0800 (China Standard Time)
Document.writeln (Date.parse ());
NaN
Document.writeln (date.parse (' 6/10/2014 '));
1402329600000 milliseconds
Document.writeln (Date.parse (' Thu of the 2015 01:25:53 '));
1420651553000 Millisecond number
Document.writeln (DATE.UTC ());
NaN
//Date Format method
Document.writeln (' <br/> ');
Document.writeln (Date.todatestring ());
Thu 2015
document.writeln (' <br/> ')
Document.writeln (date.totimestring ());
01:39:08 gmt+0800 (China Standard Time)
Document.writeln (' <br/> ')
Document.writeln (date.tolocaledatestring ( ));
January 8, 2015
document.writeln (' <br/> ')
Document.writeln (date.tolocaletimestring ());
Morning 1:39:08
Document.writeln (' <br/> ')
Document.writeln (date.toutcstring ());
Wed, 2015 17:39:08 GMT
//Some set,get methods
</script>

Method Five:

Date.prototype.Format = function (formatstr) {var str = formatstr; var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' Six ']; str = Str.replace (/yyyy|
yyyy/, This.getfullyear ()); str = str.replace (/yy| yy/, (this.getyear ()%) > 9?
(This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100)); str = str.replace (/mm/, This.getmonth () + 1) > 9?
(This.getmonth () + 1). ToString (): ' 0 ' + (this.getmonth () + 1));
str = str.replace (/m/g, (This.getmonth () + 1)); str = str.replace (/w|
W/g, Week[this.getday ()]); str = str.replace (/dd| Dd/, This.getdate () > 9?
This.getdate (). toString (): ' 0 ' + this.getdate ()); str = str.replace (/d|
D/g, This.getdate ()); str = str.replace (/hh| hh/, This.gethours () > 9?
This.gethours (). toString (): ' 0 ' + this.gethours ()); str = str.replace (/h|
H/g, This.gethours ());
str = str.replace (/mm/, This.getminutes () > 9 this.getminutes (). toString (): ' 0 ' + this.getminutes ());
str = str.replace (/m/g, This.getminutes ()); str = str.replace (/ss| ss/, This.getseconds () > 9?
This.getseconds (). toString (): ' 0 ' + this.getseconds ()); str = str.replace (/s|
S/g, This.getseconds ()); Return STR}

Method Six:

Date.prototype.Format = function (formatstr) {var str = formatstr; var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' Six ']; str = Str.replace (/yyyy|
yyyy/, This.getfullyear ()); str = str.replace (/yy| yy/, (this.getyear ()%) > 9?
(This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100)); str = str.replace (/mm/, This.getmonth () + 1) > 9?
(This.getmonth () + 1). ToString (): ' 0 ' + (this.getmonth () + 1));
str = str.replace (/m/g, (This.getmonth () + 1)); str = str.replace (/w|
W/g, Week[this.getday ()]); str = str.replace (/dd| Dd/, This.getdate () > 9?
This.getdate (). toString (): ' 0 ' + this.getdate ()); str = str.replace (/d|
D/g, This.getdate ()); str = str.replace (/hh| hh/, This.gethours () > 9?
This.gethours (). toString (): ' 0 ' + this.gethours ()); str = str.replace (/h|
H/g, This.gethours ());
str = str.replace (/mm/, This.getminutes () > 9 this.getminutes (). toString (): ' 0 ' + this.getminutes ());
str = str.replace (/m/g, This.getminutes ()); str = str.replace (/ss| ss/, This.getseconds () > 9?
This.getseconds (). toString (): ' 0 ' + this.getseconds ()); str = str.replace (/s|
S/g, This.getseconds ()); Return STR}

Method Seven:

 * * 1, < 60s, displayed as "just" 2, >= 1min && < min, showing the current time difference "xx minutes ago" 3, >= 60min && < 1day, showing the current time difference "Today xx:xx" 4, >= 1day && < 1year, show the date "xx month xx day xx:xx" 5, >= 1year, show the specific date "XXXX" XX month XX day xx:xx "/function TimeFormat (time) {var date = new Date (time), curdate = new Date (), year = Date.getfullyear (), M Onth = Date.getmonth () + ten, day = Date.getdate (), hour = Date.gethours (), minute = Date.getminutes (), curyear = curdate.g
 
Etfullyear (), Curhour = Curdate.gethours (), timestr; if (Year < curyear) {timestr = yearly + ' years ' + month + ' month ' + day + ' days ' + Hour + ': ' + Minute;}
 
else{var pasttime = curdate-date, pasth = pasttime/3600000; if (Pasth > Curhour) {timestr = month + ' month ' + day + ' days ' + Hour + ': ' + Minute;} else if (pasth >= 1) {timestr = ' today ' + Hour + ': ' + minute + ' min ';} else{var pastm = curdate.getminutes ()-minute; if (Pastm > 1) {timestr = pastm + ' minutes ago ';}
else{timestr = ' just ';}}
return timestr; }

is not the seventh kind of more personalized, personally recommended this

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.