87,994
社区成员
发帖
与我相关
我的任务
分享
<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 d = new Date() ;
alert(d.format("yyyy")) ; //年
alert(d.format("MM")) ; //月
alert(d.format("dd")) ; //日
alert(d.format("qq")) ; //季度
alert(d.format("yyyy-MM-dd")) ; //年月日
</script>
<script>
var d = new Date("Mon Apr 28 00:00:00 UTC+0900 2008");//东9区时间
alert(d);//东8区应该是2008-04-27 23:00:00
//LZ如果想要东9区时间,则加1小时即可
d.setTime(d.getTime()+60*60*1000);
alert(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate());
</script>
s = "Mon Apr 28 00:00:00 UTC+0800 2008";
s = fDate(s);
alert(s)
function fDate(s){
var dt = new Date(s);
var y = dt.getFullYear();
var m = dt.getMonth()+1;
var d = dt.getDate();
m = m<10?'0'+m:m;
d = d<10?'0'+d:d;
return y +"-"+m+"-"+d;
}