date=new
date(yyyy,mth,dd)
如果你指定日期字符串,那么就把前四个放在yyyy那里,然后是逗号",",mth:用整数表示月份,从0(1月)到11(12月),你这个是12月,就用11,dd:表示一个月中的第几天,从1到31。
你这个就应该是var
date=new
date(2011,11,31)
1. 将日期转换为指定的格式:比如转换成 年月日时分秒 这种格式:yyyy-MM-dd hh:mm:ss 或者 yyyy-MM-dd。当然是网上的方法,只是总结下。可以为Date原型添加如下的方法:
Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(),//日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
}
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length))
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)))
}
}
return fmt
}
比如我们可以这样调用下:
var time1 = new Date().format("yyyy-MM-dd hh:mm:ss")
console.log(time1)