获取近半年的开始日期和结束日期
// 获取近半年
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 lastMonthDay = new Date((year - 1), (12 - (6 - parseInt(month))), 0).getDate();
//6个月前所在月的总天数小于现在的天日期
if(lastMonthDay < day){
dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + lastMonthDay;
}else{
dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + day;
}
}else{
//6个月前所在月的总天数
var lastMonthDay = new Date(year, (parseInt(month) - 6), 0).getDate();
//6个月前所在月的总天数小于现在的天日期
if(lastMonthDay < day){
//当前天日期小于当前月总天数,2月份比较特殊的月份
if(day < nowMonthDay){
dateObj.last = year + '-' + (month - 6) + '-' + (lastMonthDay - (nowMonthDay - day));
}else{
dateObj.last = year + '-' + (month - 6) + '-' + lastMonthDay;
}
}else{
dateObj.last = year + '-' + (month - 6) + '-' + day;
}
}
console.log(dateObj.last,dateObj.now);
//设置到对应的表单内部
this.searchData.dealTime=[dateObj.last,dateObj.now]
}
获取当前日期前一个月30天的日期
/* 获取过去时间,并传值给现在时间 */
getPassMonthFormatDate() {
var nowDate = new Date();
var date = new Date(nowDate);
date.setDate(date.getDate() - 30);
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var formatDate = year + seperator1 + month + seperator1 + strDate;
this.getNowFormatDate(formatDate);
},
/* 获取现在时间,并接受过去时间的值 */
getNowFormatDate(formatDate) {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var nowData = year + seperator1 + month + seperator1 + strDate;
console.log(formatDate,nowData);
//设置到对应的form表单内部
this.searchData.dealTime=[formatDate,nowData]
},