<html>
<head>test</head>
<body>
<p>日期格式化</p>
<script>
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
var var1 = new Date();
var var2 = var1.format("yyyyMMdd");
var var3 = var2.toString();
var var4 = parseInt(var3);
var var5 = new Date("2017-03-22 13:38:37");
var var6 = new Date("Wed Mar 22 13:38:37 CST 2017");
var var7 = var5.format("yyyyMMdd");
var var8 = var6.format("yyyyMMdd");
alert(var1);//Wed Apr 19 2017 11:15:56 GMT+0800(中国标准时间)
alert(var2);//20170419
alert(var3);//20170419
alert(var4);//20170419
alert(var5);//Wed Mar 22 2017 13:38:37 GMT+0800(中国标准时间)
alert(var6);//Thu Mar 23 2017 03:38:37 GMT+0800(中国标准时间)
alert(var7);//20170322
alert(var8);//20170323
</script>
<p>测试测试</p>
</body>
</html>
<head>test</head>
<body>
<p>日期格式化</p>
<script>
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
var var1 = new Date();
var var2 = var1.format("yyyyMMdd");
var var3 = var2.toString();
var var4 = parseInt(var3);
var var5 = new Date("2017-03-22 13:38:37");
var var6 = new Date("Wed Mar 22 13:38:37 CST 2017");
var var7 = var5.format("yyyyMMdd");
var var8 = var6.format("yyyyMMdd");
alert(var1);//Wed Apr 19 2017 11:15:56 GMT+0800(中国标准时间)
alert(var2);//20170419
alert(var3);//20170419
alert(var4);//20170419
alert(var5);//Wed Mar 22 2017 13:38:37 GMT+0800(中国标准时间)
alert(var6);//Thu Mar 23 2017 03:38:37 GMT+0800(中国标准时间)
alert(var7);//20170322
alert(var8);//20170323
</script>
<p>测试测试</p>
</body>
</html>