本文給大家匯總介紹了javascript格式化日期時間的五種常用方法,個人對第五種個性化輸出時間比較有興趣,基本上只要項目中能用到都是使用第五種,推薦給小伙伴們。
示例一:
?
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> <html> <head> <meta charset="UTF-8"> <title>TT</title> <script src="jquery.js"></script> </head> <body> <p style="line-height: 50%;"><span></span></p> <input type="button" value="輕輕" onclick="syncTime()"> <script> function syncTime(){ var mydate = new Date(); var week; switch (mydate.getDay()){ case 1: week="星期一"; break; case 2: week="星期二"; break; case 3: week="星期三"; break; case 4: week="星期四"; break; case 5: week="星期五"; break; case 6: week="星期六"; break; default: week="星期天"; } var str = "" + mydate.getFullYear() + " 年 " + (mydate.getMonth() + 1) + " 月 " + mydate.getDate() + " 日 " + week; $('.todaytime')[0].innerHTML = str; str = '"savesynctime" : "' + str + '"' return str; } console.log(syncTime()); </script> </body> </html>示例二:
?
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 <script type="text/javascript"> var date = new Date(); document.writeln(date); //Thu Jan 08 2015 01:25:53 GMT+0800 (中國標准時間) document.writeln(Date.parse()); //NaN document.writeln(Date.parse('6/10/2014')); //1402329600000 毫秒數 document.writeln(Date.parse('Thu Jan 08 2015 01:25:53')); //1420651553000 毫秒數 document.writeln(Date.UTC()); //NaN //日期格式化方法 document.writeln('<br/>'); document.writeln(date.toDateString()); //Thu Jan 08 2015 document.writeln('<br/>') document.writeln(date.toTimeString()); //01:39:08 GMT+0800 (中國標准時間) document.writeln('<br/>') document.writeln(date.toLocaleDateString()); //2015年1月8日 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 //一些set,get方法 </script>示例三:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Date.prototype.Format = function(formatStr) { var str = formatStr; var Week = ['日', '一', '二', '三', '四', '五', '六']; 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 }示例四:
?
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 43 44 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={}; 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?