需求: 给出时间段
1.获取时间段中每个月的第一天,最后一天,对应年月;
2.三个属性给一个对象,所有的对象放进一个数组;
3.对象格式:
{
startDate:年-月-第一天,
endDate:年-月-最后一天,
yearMonth:年-月
}
4.开始时间所在月份要求是startDate为开始日期。结束时间所在月份要求endDate最后一天为结束日期。
5.针对该需求谢了一些草稿代码。功能已经实现,没有细改,有需求的可以自己加判断。以下是效果。网上没有找到合适这个需求的。如果有,欢迎分享。
<script>
var getYearMonth = function(startdate,enddate){
var needData = [];
var startYear = parseInt(startdate.split('-')[0]);
var startMonth = parseInt(startdate.split('-')[1]);
var endYear = parseInt(enddate.split('-')[0]);
var endMonth = parseInt(enddate.split('-')[1]);
var beginYear = startYear;
var beginMonth = startMonth;
for(var j = beginYear;j<=endYear;j++){
var temp = {};
if(j==startYear&&j!=endYear){
for(var i=beginMonth;i<13;i++){
if(i<12){
if(i==startMonth&&j==startYear){
//起始日期
temp.startD = j+'-'+i+'-'+parseInt(startdate.split('-')[2]);
temp.endD =j+'-'+i+'-'+ new Date(j,i,0).getDate();
temp.YearMonth = startYear+'-'+startMonth;
needData.push(temp);
temp = {};
}else{
//当前月的最后一天用下一月的第一天减去一天时间计算
// var nextMonthHMS =new Date(j+'-'+(i+1)+'-'+'1').getTime()-3600000*24;
// var monthLastday = new Date(nextMonthHMS).getFullYear()+'-'+(new Date(nextMonthHMS).getMonth()+1)+'-'+new Date(nextMonthHMS).getDate()
temp.startD = j+'-'+i+'-'+1;
// temp.endD = monthLastday;
temp.endD = j+'-'+i+'-'+ new Date(j,i,0).getDate();
temp.YearMonth = j+'-'+i;
needData.push(temp);
temp = {};
}
}else if(i==12&&i==startMonth){
//12月的时候最后一天就是31号
temp.startD = beginYear+'-'+12+'-'+parseInt(startdate.split('-')[2]);
temp.endD = beginYear+'-'+12+'-'+31;
temp.YearMonth = beginYear+'-'+12;
needData.push(temp);
temp = {};
}else if(i==12){
//12月的时候最后一天就是31号
temp.startD = beginYear+'-'+12+'-'+1;
temp.endD = beginYear+'-'+12+'-'+31;
temp.YearMonth = beginYear+'-'+12;
needData.push(temp);
temp = {};
}
}
}else if(j>startYear&&j<endYear){
for(var k=1;k<13;k++){
if(k<12){
temp.startD = j+'-'+k+'-'+1;
temp.endD = j+'-'+k+'-'+new Date(j,k,0).getDate();
temp.YearMonth = j+'-'+k;
needData.push(temp);
temp = {};
}else if(k==12){
temp.endD = j+'-'+k+'-'+new Date(j,k,0).getDate();
temp.startD = j+'-'+k+'-'+1;
temp.YearMonth = j+'-'+k;
needData.push(temp);
temp = {};
}
}
}else if(j==endYear&&j!=startYear){
for(var k=1;k<=endMonth;k++){
if(k==endMonth){
temp.startD = j+'-'+k+'-'+'1';
temp.endD = j+'-'+k+'-'+parseInt(enddate.split('-')[2]);
temp.YearMonth = j+'-'+k;
needData.push(temp);
temp = {};
}else{
if(k<12){
temp.startD = j+'-'+k+'-'+1;
temp.endD = j+'-'+k+'-'+new Date(j,k,0).getDate();
temp.YearMonth = j+'-'+k;
needData.push(temp);
temp = {};
}else if(k==12){
temp.startD = j+'-'+k+'-'+1;
temp.endD = j+'-'+k+'-'+31;
temp.YearMonth = j+'-'+k;
needData.push(temp);
temp = {};
}
}
}
}else if(j==endYear&&j==startYear){
for(var r=startMonth;r<=endMonth;r++){
if(r==startMonth){
temp.startD = j+'-'+r+'-'+parseInt(startdate.split('-')[2]);
temp.endD =j+'-'+r+'-'+ new Date(j,r,0).getDate();
temp.YearMonth = startYear+'-'+startMonth;
needData.push(temp);
temp = {};
}else if(r==endMonth){
temp.startD = j+'-'+r+'-'+'1';
temp.endD = j+'-'+r+'-'+parseInt(enddate.split('-')[2]);
temp.YearMonth = j+'-'+r;
needData.push(temp);
temp = {};
}else{
if(r<12){
temp.startD = j+'-'+r+'-'+1;
temp.endD = j+'-'+r+'-'+1;
temp.YearMonth = j+'-'+r;
needData.push(temp);
temp = {};
}else if(r==12){
temp.startD = j+'-'+r+'-'+1;
temp.endD = j+'-'+r+'-'+31;
temp.YearMonth = j+'-'+r;
needData.push(temp);
temp = {};
}
}
}
}
}
console.log(needData);
}
getYearMonth('2020-12-02','2021-2-14');
</script>