Javascript formatted Date and Time method summary, javascript date
Example 1:
<! DOCTYPE html>
Example 2:
<Script type = "text/javascript"> var date = new Date (); document. writeln (date); // Thu Jan 08 2015 01:25:53 GMT + 0800 (China Standard Time) document. writeln (Date. parse (); // NaN document. writeln (Date. parse ('2014/1/123'); // 6/10 ms document. writeln (Date. parse ('thu Jan 08 2015 01:25:53 '); // 1420651553000 Ms document. writeln (Date. UTC (); // NaN // Date formatting method document. writeln ('<br/>'); document. writeln (date. toDateString (); // Thu Jan 08 2015 document. writeln ('<br/>') document. writeln (date. toTimeString (); // 01:39:08 GMT + 0800 (China Standard Time) document. writeln ('<br/>') document. writeln (date. toLocaleDateString (); // October January 8, 2015 document. writeln ('<br/>') document. writeln (date. toLocaleTimeString (); // 1:39:08 document. writeln ('<br/>') document. writeln (date. toUTCString (); // Wed, 07 Jan 2015 17:39:08 GMT // some set and get methods </script>
Example 3:
Date. prototype. format = function (formatStr) {var str = formatStr; var Week = ['day', 'yi', '2', '3', '4', '5 ', '6']; str = str. replace (/yyyy | YYYY/, this. getFullYear (); str = str. replace (/yy | YY/, (this. getYear () % 100)> 9? (This. getYear () % 100 ). 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 4:
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 = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug ", "Sep", "Oct", "Nov", "Dec"]; var MMMArr = ["May January", "May February", "May March", "May April ", "August", "August"]; var WeekArrEn = ["Sun", "Mon", "Tue", "Web", "Thu", "Fri", "Sat"]; var WeekArr = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; var oNumber = time. milliseco Nd/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: 52console. log (d. toString (""); // 2014-7-27 9: 26: 52console. log (d. toString ("yyyy-MM-dd HH: mm: ss"); // 2014-07-27 09: 26: 52console. log (d. toString ("MM dd, yyyy); // July 27, 2014: 52console. log (d. toString ("yyyy-MM-dd HH: mm: ss fff"); // 2014-07-27 09:26:52 237console. log (d. toString ("yyyy MMM dd EEE"); // The console on Friday, January 1, July 27, 2014. log (d. toString ("yyyy MMM dd EEE", "en"); // 2014 Jul 27 Fri
Example 5: Time Personalized output function
/* 1, <60 s, displayed as "just" 2,> = 1 min & <60 min, display and current time difference "XX minutes before" 3,> = 60 min & <1day, display and current time difference "Today XX: XX "4,> = 1day & <1 year, Show Date" XX month XX: XX "5,> = 1 year, display the specific date "XXXX Year XX month XX day XX: XX" */function timeFormat (time) {var Date = new Date (time), curDate = new date (), year = date. getFullYear (), month = date. getMonth () + 10, day = date. getDate (), hour = date. getHours (), minute = date. getMinutes (), curYear = curDate. getFullYear (), curHour = curDate. getHours (), timeStr; if (year <curYear) {timeStr = year + 'Year' + month + 'month' + day + 'day' + hour + ': '+ minute;} else {var pastTime = curDate-date, Path = pastTime/3600000; if (path> curHour) {timeStr = month + 'month' + day + 'day' + hour + ':' + minute;} else if (pasomething> = 1) {timeStr = 'today '+ hour +': '+ minute +' minute ';} else {var pastM = curDate. getMinutes ()-minute; if (pastM> 1) {timeStr = pastM + 'minutes ago ';} else {timeStr = 'gang' ;}} return timeStr ;}
The above is all the content of this article. I hope you will like it.