您的位置:首页 > Web前端 > JavaScript

JS插件(1) 实现 指定格式日期

2014-08-28 00:01 585 查看
<!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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息