/* * function: 日期格式化 * format: 格式 * return: 返回格式化后的日期 * */ 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; }; /* * function: 日期加减 * decDay: 天数(正或负) * return: 返回加减后的时间 * */ Date.prototype.add=function(decDay){//日期加减 var day=1000*60*60*24; return new Date(this.valueOf()+decDay*day); }; var date=new Date().add(-2).format('yyyyMMdd'); console.log(date);