javascript轉(zhuǎn)換時間的方法:創(chuàng)建一個【util.js】文件,在里面重新封裝一下Date的format方法,代碼為【let date = new Date().Format("yyyy年M月dd日tEEEtHH:mm:ss")】。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版,DELL G3電腦。
javascript轉(zhuǎn)換時間的方法:
創(chuàng)建一個util.js文件,在里面重新封裝一下Date的format方法:
//Date的prototype 屬性可以向?qū)ο筇砑訉傩院头椒ā? Date.prototype.Format = function(fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours() % 12 === 0 ? 12 : this.getHours() % 12, //小時 "H+": this.getHours(), //小時 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; var week = { "0": "u65e5", "1": "u4e00", "2": "u4e8c", "3": "u4e09", "4": "u56db", "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 > 1) ? (RegExp.$1.length > 2 ? "u661fu671f" : "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]) : (("00" + o[k]).substr(("" + o[k]).length))); } } return fmt; };
具體調(diào)用的時候就可以變得很方便,想要什么格式就可以形成什么格式,例如 年月日 星期幾 時分秒,做個簡單的例子,如果有哪里看不明白可以評論,我看見會給你解釋的。
let date = new Date().Format("yyyy年MM月dd日tEEEtHH:mm:ss"); $("#date-now").html(date);
相關(guān)免費(fèi)學(xué)習(xí)推薦:javascript學(xué)習(xí)教程