html
<yto-form-item label=" ">
<div>
<yto-radio-group v-model="reportDateType" size="mini" @change="handleChangeReport('0')" class="all-time-position">
<yto-radio-button label="date">日</yto-radio-button>
<yto-radio-button label="week">周</yto-radio-button>
<yto-radio-button label="month">月</yto-radio-button>
</yto-radio-group>
<yto-date-picker ref="reoprtDatePicker" :clearable="true"
v-model="reportDate"
size="mini"
:picker-options="pickerOptions"
:key="reportDateType"
:type="reportDateType"
:format="reportDateType==='week'?'yyyy 第 WW 周':null"
:placeholder="reportDateType==='date'?'请选择日':reportDateType==='week'?'请选择周':reportDateType==='month'?'请选择月': ''"
class="statistic-analysis-report-spot-time-select" @change="handleChangeReport()">
</yto-date-picker>
</div>
</yto-form-item>
这个js里面写dateFormat 方法
import { dateFormat } from '@/util/util.js';
方法如下
export function dateFormat(date, fmt) {
var o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'h+': date.getHours(), // 小时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
'S': date.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
}
}
return fmt;
}
data里面定义变量
//查询列表
listForm: {
startTime:'', //开始时间
endTime:'', //结束时间
},
activeName: 'package_timeliness',
reportDateType: 'date',
reportDate: null,
timeObj:{}, //存日,周,月时间的
pickerOptions: {
disabledDate(date) {
// 获取当前日期
var currentDate = new Date();
// 获取前一天日期
var previousDate = new Date();
previousDate.setDate(currentDate.getDate() - 1); //获取t-1天
return date.getTime() > previousDate; //禁用
}
},
created
created() {
this.$nextTick(()=>{
this.handleChangeReport('0')//默认查t-1天数据
this.getList();//列表
})
},
methods
methods: {
//时间
handleChangeReport(type) {
let self=this;
if(type=='0'){
// 获取当前日期
var currentDate = new Date();
// 获取前一天日期
var previousDate = new Date();
previousDate.setDate(currentDate.getDate() - 1); //获取t-1天
self.reportDate = previousDate
}else if(type=='1'){
}
self.timeDifferenceReport(self.reportDate, self.reportDateType);
},
timeDifferenceReport() {
let time = new Date(dateFormat(this.reportDate, "yyyy-MM-dd")); // 可入参指定时间
let year = time.getFullYear();
let month = time.getMonth();
let reportDate = time.getDate(); // 获取日期
let day = time.getDay(); // 获取周几,0-6,0表示周日
let _day = day == 0 ? 7 : day;
switch (this.reportDateType) {
case "date":
this.timeObj = {
startTime: `${dateFormat(this.reportDate, "yyyy-MM-dd")} 00:00:00`,
endTime: `${dateFormat(this.reportDate, "yyyy-MM-dd")} 23:59:59`,
};
break;
case "week":
// 获取周一日期
var startDate = new Date(year, month, reportDate - _day + 1);
// 获取周日日期
var endDate = new Date(
new Date(startDate).getTime() + 6 * 24 * 3600 * 1000
);
this.timeObj = {
startTime: `${dateFormat(startDate, "yyyy-MM-dd")} 00:00:00`,
endTime: `${dateFormat(endDate, "yyyy-MM-dd")} 23:59:59`,
};
break;
case "month":
var startDate = new Date(year, month, 1);
var endDate = new Date(year, month + 1, 0);
this.timeObj = {
startTime: `${dateFormat(startDate, "yyyy-MM-dd")} 00:00:00`,
endTime: `${dateFormat(endDate, "yyyy-MM-dd")} 23:59:59`,
};
}
console.log('this.timeObj: ', this.timeObj);
return this.timeObj;
},
}
列表
//列表
getList(metricsCode) {
this.listLoading = true;
let param = Object.assign(this.listForm, {
page: this.currentPage,
limit: this.pageSize,
startTime:this.timeObj.startTime || '',
endTime:this.timeObj.endTime || '',
metricsCode:metricsCode,
});
console.log('参数对不对: ', param);
http.post("/joinWarehouse/page", param).then((res) => {
if (res.status == 200) {
this.tableData = res.data.rows || [];
this.total = Number(res.data.total || 0);
} else {
this.$notify.error({
title: "失败",
message: res.message,
});
this.tableData = [];
this.total = 0;
}
this.listLoading = false;
});
},