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

<!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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值