JavaScript format Date-time method rollup

Source: Internet
Author: User
Tags current time getdate min

This article gives you a summary of JavaScript format date time of the five common methods, individuals on the fifth personalized output time is more interested, basically as long as the project can use the fifth, recommended to the small partners.

Example one:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <! DOCTYPE html>

Example two:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 <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 08 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>

Example three:

?

1 2 3 4 5 6 7 8 9" 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}

Example four:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67-68 Date.prototype.tostring=function (format,loc) {var time={}; Year=this.getfullyear (); Time. Tyear= ("" +time). Year). substr (2); Time. Month=this.getmonth () +1; Time. Tmonth=time. Month<10? " 0 "+time. Month:time. Month; Time. Day=this.getdate (); Time. Tday=time. Day<10? " 0 "+time. Day:time. Day; Time. Hour=this.gethours (); Time. Thour=time. Hour<10? " 0 "+time. Hour:time. Hour; Time.hour=time. Hour<13?time. Hour:time. Hour-12; Time. Thour=time.hour<10? " 0 "+time.hour:time.hour; Time. Minute=this.getminutes (); Time. Tminute=time. Minute<10? " 0 "+time. Minute:time. Minute; Time. Second=this.getseconds (); Time. Tsecond=time. Second<10? " 0 "+time. Second:time. Second; Time.millisecond=this.getmilliseconds (); Time. Week=this.getday ();   var mmmarren=[, "Feb", "Mar", "APR", "may", "June", "O", "Aug", "Sep", "Oct", "Nov", "Dec"]; var mmmarr=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var weekarren=["Sun", "Mon", "Tue", "Web", "Thu", "Fri", "Sat"]; var weekarr=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];   Var onumber=time. millisecond/1000;   if (format!=undefined && format.replace (/s/g, ""). Length>0) {if (loc!=undefined && loc = "en") {mmmarr=mmmarren.slice (0); Weekarr=weekarren.slice (0); } format=format. replace (/yyyy/ig,time. year). Replace (/yyy/ig,time. year). Replace (/yy/ig,time. tyear). replace (/y/ig,time. tyear). replace (/mmm/g,mmmarr[time. MONTH-1]). replace (/mm/g,time. tmonth). replace (/m/g,time. Month). replace (/dd/ig,time. tday). replace (/d/ig,time. Day). Replace (/hh/g,time. Thour). replace (/h/g,time. Hour). replace (/hh/g,time. Thour). Replace (/h/g,time.hour). replace (/mm/g,time. Tminute). replace (/m/g,time. Minute). replace (/ss/ig,time. Tsecond). replace (/s/ig,time. Second). replace (/fff/ig,time. millisecond). Replace (/ff/ig,onumber.tofixed (2) *100). Replace (/f/ig,onumber.tofixed (1) *10). Replace (/eee/g, Weekarr[time. Week]); } else{format=time. year+ "-" +time. month+ "-" +time. Day+ "" +time. hour+ ":" +time. Minute+ ":" +time. Second; } return format; }   var d=new Date (); Console.log (D.tostring ()); 2014-7-27 9:26:52 Console.log (d.tostring ("")); 2014-7-27 9:26:52 Console.log (d.tostring ("Yyyy-mm-dd HH:mm:ss")); 2014-07-27 09:26:52 Console.log (d.tostring ("YYYY year mm month DD Day HH:mm:ss"); July 27, 2014 09:26:52 Console.log (d.tostring ("Yyyy-mm-dd HH:mm:ss FFF")); 2014-07-27 09:26:52 237 Console.log (d.tostring ("yyyy MMM DD EEE")); July 2014 27 weeks Five Console.log (d.tostring ("yyyy MMM DD EEE", "en")); 2014 June Fri

Example five: Time personalization output feature

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 * * 1, < 60s, displayed as "just" 2, >= 1min && < min, showing the current time difference "xx minutes ago" 3, >= 60m In && < 1day, showing the current time difference "Today xx:xx" 4, >= 1day && < 1year, display date "XX month xx day xx:xx" 5, >= 1year, show the specific date "X" xxx xx month xx day xx:xx "*/function TimeFormat (time) {var date = new Date (time), curdate = new Date (), year = Date.getfullyear () , month = Date.getmonth () + ten, day = Date.getdate (), hour = Date.gethours (), minute = Date.getminutes (), curyear = Curdat E.getfullyear (), Curhour = Curdate.gethours (), timestr;   if (Year < curyear) {timestr = years + ' + ' + month + ' month ' + day + ' days ' + Hour + ': ' + Minute;} else{var pasttime = curdate-date, pasth = pasttime/3600000;   if (Pasth > Curhour) {timestr = month + ' month ' + Day + ' Day ' + 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; }

The above is the entire contents of this article, I hope you can enjoy.

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.