// 獲取上個月的時間 getLastMonth() { var nowdays = new Date(); var year = nowdays.getFullYear(); var month = nowdays.getMonth(); if (month == 0) { month = 12; year = year - 1; } if (month < 10) { month = '0' + month; } var myDate = new Date(year, month, 0); var startDate = year + '-' + month + '-01'; //上個月第一天 var endDate = year + '-' + month + '-' + myDate.getDate() ; //上個月最后一天 this.setData({ 'options[0].value':startDate+'-'+endDate }) },
// 獲取近三個月 往前推90天 getLastthreeMonth(){ var now = new Date(); var year = now.getFullYear(); //0-11表示1-12月 var month = now.getMonth() + 1; var day = now.getDate(); var dateObj = {}; dateObj.now = year + '-' + month + '-' + day; //當前月的總天數 var nowMonthDay = new Date(year, month, 0).getDate(); //如果是1、2、3月,年數往前推一年 if(month - 3 <= 0){ //3個月前所在月的總天數 var last3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate(); //3個月前所在月的總天數小於現在的天日期 if(last3MonthDay < day){ dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + last3MonthDay; }else{ dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + day; } }else{ //3個月前所在月的總天數 var last3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate(); //3個月前所在月的總天數小於現在的天日期 if(last3MonthDay < day){ //當前天日期小於當前月總天數,2月份比較特殊的月份 if(day < nowMonthDay){ dateObj.last = year + '-' + (month - 3) + '-' + (last3MonthDay - (nowMonthDay - day)); }else{ dateObj.last = year + '-' + (month - 3) + '-' + last3MonthDay; } }else{ dateObj.last = year + '-' + (month - 3) + '-' + day; } } this.setData({ 'options[1].value':dateObj.last+'-'+dateObj.now }) },
// 獲取近半年 getLastHalfYear(){ var now = new Date(); var year = now.getFullYear(); //0-11表示1-12月 var month = now.getMonth() + 1; var day = now.getDate(); var dateObj = {}; dateObj.now = year + '-' + month + '-' + day; //當前月的總天數 var nowMonthDay = new Date(year, month, 0).getDate(); //如果是1、2、3,4,5,6月,年數往前推一年 if(month - 6 <= 0){ //6個月前所在月的總天數 var last3MonthDay = new Date((year - 1), (12 - (6 - parseInt(month))), 0).getDate(); //6個月前所在月的總天數小於現在的天日期 if(last3MonthDay < day){ dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + last3MonthDay; }else{ dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + day; } }else{ //6個月前所在月的總天數 var last3MonthDay = new Date(year, (parseInt(month) - 6), 0).getDate(); //6個月前所在月的總天數小於現在的天日期 if(last3MonthDay < day){ //當前天日期小於當前月總天數,2月份比較特殊的月份 if(day < nowMonthDay){ dateObj.last = year + '-' + (month - 6) + '-' + (last3MonthDay - (nowMonthDay - day)); }else{ dateObj.last = year + '-' + (month - 6) + '-' + last3MonthDay; } }else{ dateObj.last = year + '-' + (month - 6) + '-' + day; } } this.setData({ 'options[2].value':dateObj.last+'-'+dateObj.now }) }