<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <script> //一个 Date 的扩展方法,用于实现Java的SimpleDateFormat //用法 new Date().formatDate("yyyy年MM月dd日 HH:mm:ss"); //大小写遵循 java 的日期格式 Date.prototype.formatDate=function(format){ var calc=function(num){ if(num<10){ return "0"+num; }else{ return num; } } var _date=format; var day=calc(this.getDate()); var fullDay=this.getDate(); var month=this.getMonth()+1; var fullMonth=calc(this.getMonth()+1); var year=this.getFullYear(); var fullHour =this.getHours(); var hour=fullHour>12? "0"+fullHour-12: fullHour; var minute =this.getMinutes(); var second =this.getSeconds(); _date=_date.replace("yyyy",year) .replace("MM",fullMonth) .replace("M",month) .replace("dd",fullDay) .replace("d",day) .replace("HH",fullHour) .replace("hh",hour) .replace("mm",minute) .replace("ss",second);
return _date; } //使用 alert(new Date().formatDate("yyyy年MM月dd日 hh:MM:ss")); </script> <title>Document</title> </head> </html> </p>