自动生成两个日期之间所有的年月

 dataMonth(value) {
//        var demo=this.value
                var getDate = function (str) {
                    var tempDate = new Date();
                    var list = str.split("-");
                    tempDate.setFullYear(list[0]);
                    tempDate.setMonth(list[1] - 1);
                    tempDate.setDate(list[2]);
                    return tempDate;
                }
                var date2 = new Date();
                var date1 = getDate(value);

                if (date1 > date2) {
                    var tempDate = date1;
                    date1 = date2;
                    date2 = tempDate;
                }
                // date1.setDate(date1.getDate() + 1);
                var dateArr = [];
                var i = 0;
                while (
                    !(date1.getFullYear() == date2.getFullYear()
                        && date1.getMonth() == date2.getMonth()
                        && date1.getDate() == date2.getDate())
                    ) {
                    var dayStr = date1.getDate().toString();
                    if (dayStr.length == 1) {
                        dayStr = "0" + dayStr;
                    }
                    dateArr[i] = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-"
                        + dayStr;
                    i++;
                    date1.setDate(date1.getDate() + 1);
                }

                var dayStr = date2.getDate().toString();
                if (dayStr.length == 1) {
                    dayStr = "0" + dayStr;
                }
                dateArr.push(date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + dayStr);
                var dates = [];
                for (var j = 0; j <= dateArr.length; j++) {
                    if ((j + 1) < dateArr.length) {
                        var a = dateArr[j].split("-");
                        var b = dateArr[j + 1].split("-");
                        if (a[1] != b[1]) {
                            dates.push(dateArr[j])
                        }
                    }
                }
                dates.push(date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + dayStr)
                this.newDate = [];
                for (var x = 0; x < dates.length; x++) {
                    var arr = dates[x].split('-');
                    this.newDate.push(arr[0] + '-' + arr[1]);
                }
//  console.log(newDate)

                //会返回一个日期数组  包含开始和结束时间之间的所有月份始末日期
                this.times = []
                for (var i = 0; i < this.newDate.length; i++) {
                    this.times.push({"name": this.newDate[i], "method": this.gettime})
                }
            },

 

posted @ 2018-08-20 10:11  随意滴博客  阅读(467)  评论(0编辑  收藏  举报