selectDate(item, index) {
console.log('选中的日期', item);
this.currentDate = index;
this.currentDateType = this.dateType.filter((k, i) => {
if (index == i) {
return k;
}
})[0].value;
console.log('this.currentDateType=>', this.currentDateType);
var t = this.$moment().format('YYYY-MM-DD HH:mm:ss');
console.log('当前时间=>', t);
if (this.currentDateType == 'date') {
this.currentValue = this.$moment().format('YYYY-MM-DD');
this.reportBeginTime = this.currentValue + '00:00:00';
this.reportTime = this.currentValue + '23:59:59';
this.regionId = 1;
this.reportType = 1;
} else if (this.currentDateType == 'month') {
this.currentValue = this.$moment().format('YYYY-MM');
this.regionId = '1';
this.reportType = 3;
var fd = this.$moment()
.startOf('month')
.format('YYYY-MM-DD');
var ld = this.$moment()
.endOf('month')
.format('YYYY-MM-DD');
console.log('本月第一天', fd, '本月最后一天', ld);
this.eportBeginTime = fd + '00:00:00';
this.reportTime = ld + '23:59:59';
} else if (this.currentDateType == 'year') {
this.currentValue = this.$moment().format('YYYY');
this.regionId = 1;
this.reportType = 4;
var fy = this.$moment()
.startOf('year')
.format('YYYY-MM-DD');
var ly = this.$moment()
.endOf('year')
.format('YYYY-MM-DD');
console.log('本年第一天', fy, '本年最后一天', ly);
this.eportBeginTime = fy + '00:00:00';
this.reportTime = ly + '23:59:59';
}
if (index == 1) {
this.regionId = 1;
this.reportType = 2;
var y = this.$moment().format('YYYY');
this.curWeek = y + '-' + this.$moment().week() + '周';
const wd = this.$moment().format('E');
var f = this.$moment()
.subtract(wd - 1, 'days')
.format('YYYY-MM-DD');
var l = this.$moment()
.add(7 - wd, 'days')
.format('YYYY-MM-DD');
console.log('本周一', f, '本周日', l);
this.reportBeginTime = f + '00:00:00';
this.reportTime = l + '23:59:59';
}
}
(2)js本月第一天与最后一天
getCurrentMonthFirstDay() {
let date = new Date();
date.setDate(1);
let month = parseInt(date.getMonth() + 1);
let day = date.getDate();
if (month < 10) {
month = "0" + month;
}
if (day < 10) {
day = "0" + day;
}
this.curDateS = date.getFullYear() + "-" + month + "-" + day;
},
getNowFormatDate() {
var dateE = new Date();
let currentMonth = dateE.getMonth();
let nextMonth = ++currentMonth;
let nextMonthFirstDay = new Date(dateE.getFullYear(), nextMonth, 1);
let oneDay = 1000 * 60 * 60 * 24;
let lastTime = new Date(nextMonthFirstDay - oneDay);
let month = parseInt(lastTime.getMonth() + 1);
let day = lastTime.getDate();
if (month < 10) {
month = "0" + month;
}
if (day < 10) {
day = "0" + day;
}
this.curDateE = dateE.getFullYear() + "-" + month + "-" + day;
},
(3) 方法1:近2天、3天、7天
<FormItem label="选择时间">
<Date-picker
transfer
format="yyyy-MM-dd HH:mm:ss"
v-model="formData.dateTime"
:i18n="$i18n"
type="datetimerange"
noClear
@on-change="timeChange"
></Date-picker>
</FormItem>
import { dateFormat } from 'utilsTime';
handleTimeTransfer(param) {
let ONE_DAY = 1000 * 60 * 60 * 24;
let endTimeStr = new Date();
let startTimeStr = new Date(+endTimeStr - (param - 1) * ONE_DAY);
startTimeStr = dateFormat(startTimeStr, 'yyyy-MM-dd');
endTimeStr = dateFormat(endTimeStr, 'yyyy-MM-dd');
let params = {
start: startTimeStr + ' 00:00:00',
end: endTimeStr + ' 23:59:59'
};
let dateTime = [`${params.start}`, `${params.end}`];
console.log('时间params', params, dateTime);
this.formData.startDateStr = dateTime[0];
this.formData.endDateStr = dateTime[1];
this.formData.dateTime = dateTime;
console.log('赋值给组件dateTime', dateTime);
return dateTime;
},
(4)方法2:近2天、3天、7天
timeHandle(item, index) {
this.currentSelect = index;
var days = this.$moment().daysInMonth();
this.startTimeStr = this.$moment().format('YYYY-MM-DD');
if (item.value == 1) {
this.endTimeStr = this.$moment().format('YYYY-MM-DD');
this.isShowDate = false;
} else if (item.value == 2) {
this.endTimeStr = this.$moment()
.add(2, 'd')
.format('YYYY-MM-DD');
this.currentTime =
this.$moment().format('YYYY-MM-DD') + ' ~ ' + this.endTimeStr + '(近3天)';
this.isShowDate = false;
} else if (item.value == 3) {
this.endTimeStr = this.$moment()
.add(6, 'd')
.format('YYYY-MM-DD');
this.currentTime =
this.$moment().format('YYYY-MM-DD') + ' ~ ' + this.endTimeStr + '(近7天)';
this.isShowDate = false;
} else if (item.value == 4) {
this.endTimeStr = this.$moment()
.add(days - 1, 'd')
.format('YYYY-MM-DD');
this.currentTime =
this.$moment().format('YYYY-MM-DD') + ' ~ ' + this.endTimeStr + '(近一月)';
this.isShowDate = false;
} else {
this.isShowDate = true;
}
this.getData();
},
(5)本2小时、4小时、8小时
timeChange(label) {
if (label == '2小时') {
var t = this.$moment().format('YYYY-MM-DD HH:mm:ss');
var t1 = this.$moment(t)
.subtract(-2, 'hour')
.format('YYYY-MM-DD HH:mm:ss');
this.startTimeStr = t;
this.endTimeStr = t1;
} else if (label == '4小时') {
var t = this.$moment().format('YYYY-MM-DD HH:mm:ss');
var t1 = this.$moment(t)
.subtract(-4, 'hour')
.format('YYYY-MM-DD HH:mm:ss');
this.startTimeStr = t;
this.endTimeStr = t1;
} else {
var t = this.$moment().format('YYYY-MM-DD HH:mm:ss');
var t1 = this.$moment(t)
.subtract(-8, 'hour')
.format('YYYY-MM-DD HH:mm:ss');
this.startTimeStr = t;
this.endTimeStr = t1;
}
},
(6)js实现近7天
/获取最近7天日期
getDay(0);
getDay(-7);
getDay(0);
getDay(-3);
doHandleMonth(month) {
var m = month;
if (month.toString().length == 1) {
m = '0' + month;
}
return m;
},
getDay(day) {
var today = new Date();
var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
today.setTime(targetday_milliseconds);
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = this.doHandleMonth(tMonth + 1);
tDate = this.doHandleMonth(tDate);
return tYear + '-' + tMonth + '-' + tDate;
},
getTime() {
this.startTimeStr = this.getDay(-6);
this.endTimeStr = this.getDay(0);
},