首先,我们创建个需要加减的日期变量:
var dateTime=new Date();
加一天
dateTime=dateTime.setDate(dateTime.getDate()+1);
dateTime=new Date(dateTime);
根据时间段内的天数,显示每天
var date1 = new Date(elem.fromDate).getTime();
var date2 = new Date(elem.toDate).getTime();
var diff = date2 - date1;
var daynum = Math.floor(diff / 1000 / 60 / 60 / 24)+1;//时间段内天数
for(var i=0;i<daynum;i++){
var dateTime = new Date(elem.fromDate);//活得天
dateTime=dateTime.setDate(dateTime.getDate()+i);//設置後一天日期
dateTime=new Date(dateTime).format("MM月dd日");
newcolumns.push({
"field":dateTime,
"title":dateTime,
"align":"center",
"width": 100,
});
}
完整的日、月、年、时间段
if(elem.dateType == 0){
for(var i=0;i<24;i++){
newcolumns.push({
"field":i+"时",
"title":i+"时",
"align":"center",
"width":100,
})
}
}else if(elem.dateType == 2){
var dateNum = new Date(elem.fromDate.split("-")[0],elem.fromDate.split("-")[1],0).getDate();//获取某年下的某月的天数
for(var i=0;i<dateNum;i++){
newcolumns.push({
"field":(i+1)+"日",
"title":(i+1)+"日",
"align":"center",
"width":100,
});
}
}else if(elem.dateType == 5){//年
for(var i=1;i<13;i++){
newcolumns.push({
"field":i+"月",
"title":i+"月",
"align":"center",
"width": 100,
});
}
}else{//时间段
var date1 = new Date(elem.fromDate).getTime();
var date2 = new Date(elem.toDate).getTime();
var diff = date2 - date1;
var daynum = Math.floor(diff / 1000 / 60 / 60 / 24)+1;//时间段内天数
for(var i=0;i<daynum;i++){
var dateTime = new Date(elem.fromDate);
dateTime=dateTime.setDate(dateTime.getDate()+i);
dateTime=new Date(dateTime).format("MM月dd日");
newcolumns.push({
"field":dateTime,
"title":dateTime,
"align":"center",
"width": 100,
});
}
}