Example one:
Example two:
<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:
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:
Date.prototype.tostring=function (format,loc) {var time={}; 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", "may", "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
/* 1, < 60s, shown as "just" 2, >= 1min && < min, display with the current time difference "xx minutes ago" 3, >= 60min && < 1day, show with the current time lag " Today Xx:xx "4, >= 1day && < 1year, display the date" XX month xx day xx:xx "5, >= 1year, show the specific date" XXXX xx month XX day xx:xx "/function time Format (time) {var date = new date, curdate = new Date (), year = Date.getfullyear (), month = Date.getmo Nth () + ten, day = Date.getdate (), hour = Date.gethours (), minute = Date.getminutes (), curyear = curdate.ge
Tfullyear (), Curhour = Curdate.gethours (), timestr;
if (Year < curyear) {timestr = years + ' annual ' + 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; }
The above is the entire contents of this article, I hope you can enjoy.