中文(繁体)
時間和日期的各種格式的效果代碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <meta http-equiv="Content-Type" content= "text/html; charset=gb2312"> <TITLE>emu 的日期簡單日期格式化程式</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT="emu" > <META NAME="關鍵字" CONTENT="日期格式化程式格式emu"> <META NAME="描述" CONTENT="將日期物件格式化為字串的函數"> </HEAD> <BODY> < SCRIPT LANGUAGE="JavaScript"> < !-- Date.prototype.format = function (format){ if (format == null) format = "yyyy/MM/dd HH:mm:ss.SSS"; var 年 = this.getFullYear(); var 月份 = this.getMonth(); var sMonth = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月“][月]; var date = this.getDate(); var day = this.getDay(); var hr = this.getHours(); var min = this.getMinutes(); var sec = this.getSeconds(); var daysInYear = Math.ceil((this-new Date(year,0,0))/86400000); var weekInYear = Math.ceil((daysInYear+new Date(year,0,1).getDay())/7); var weekInMonth = Math.ceil((date+new Date(年,月,1).getDay())/7); return format.replace("yyyy",year).replace("yy",year.toString().substr(2)).replace("dd",(date<10?"0":"")+date ).replace("HH",(hr<10?"0":"")+hr).replace("KK",(hr%12<10?"0":"")+hr%12).替換("kk",(hr>0&&hr<10?"0":"")+(((hr+23)%24)+1)).replace("hh",(hr>0&&hr<10|| hr>12&&hr<22?"0":"")+(((hr+11)%12)+1)).replace("mm",(min<10?"0":"")+min) .replace("ss",(秒<10?"0":"")+sec).replace("SSS",this%1000).replace("a",(hr<12?"AM":" PM")).replace("W",weekInMonth).replace("F",Math.ceil(date/7)).replace(/E/g,["星期日","星期一","星期二" , "星期三","星期四","星期五","星期六"][日]).replace("D",daysInYear).replace("w",weekInYear).replace(/MMMM+/,sMonth).replace ( "MMM",sMonth.substring(0,3)).replace("MM",(月<9?"0":"")+(月+1)); } var d = new Date(); alert(d.format());//預設格式alert(d.format("MM-dd-yyyy"));警報(d.format(「日/月/年」));警報(d.format(“dd\MM\yyyy HH:mm ss.SSS”)); alert(d.format("yyyy年MM月dd日HH時mm分ss.SSS秒")); Alert(d.format("yyyy年MM月dd日 E HH(0~23)時mm分ss.SSS秒")); Alert(d.format("yyyy年MM月dd日E KK(0~11)時mm分ss.SSS秒a")); Alert(d.format("yyyy年MM月dd日E hh(1~12)時mm分ss.SSS秒a")); alert(d.format("yyyy年MM月dd日E kk(1~24)時mm分ss.SSS秒今年的第D天,第w週,MMMM這個月的第W週,MMMM這個月的第3週) F個E")); //--> </SCRIPT> </BODY> </HTML>