根据已知日期(yyyy-MM-dd)获取前n天的日期区间
//获取天 var pubTime="2017-12-30" function buildDay(num){ num=num-1; var myDate = new Date(pubTime); //获取今天日期 myDate.setDate(myDate.getDate() - num); var dateArray = []; var dateTemp; var flag = 1; for (var i = 0; i < num; i++) { var month=myDate.getMonth()+1 if(month<10){ month="0"+month; } var day=myDate.getDate() if(day<10){ day="0"+day; } dateTemp =myDate.getFullYear()+""+ month+""+day; dateArray.push(dateTemp); myDate.setDate(myDate.getDate() + flag); } dateArray.push(pubTime.replace(/-/g,"")) return dateArray }
//获取月分
function buildMonth(months){
var now = new Date(pubTime);
var cur_month = now.getMonth();
now.setDate(1);//重置成1号,原因:在做减月份时,减去的月份没有31号,会自动加1到下个月份,导致计算失误
if(months < 0){
var _m = cur_month+months + 1;
now.setMonth(_m);
}
var _months = [];
for(var i = 0 ; i < Math.abs(months) ; i++){
var year = now.getFullYear();
var month = now.getMonth();
if(i != 0 ){
now.setMonth(month+1);
}
year = now.getFullYear();
var _month = now.getMonth()+1;
if(_month < 10) _month = '0'+_month;
else _month = _month.toString();
_months.push(year+_month);
}
//console.log('buildMonthData============>'+_months);
return _months;
},
请爱好前端技术的朋友,联系我,有问题大家一起讨论