利用dayj转换查询时间
queryForm 查询参数对象
switch 区分选择时间类型 日 周 月 年
计算结束时间
dayjs(element).endOf("week").format("YYYY-MM-DD") 当前周结束时间
日期时间查询框配置参数格式
{
label: "",
width: 220,
key: "timeArr",
renderConfig: {
type: "DatePicker",
attributes: {
startPlaceholder: "开始日期",
endPlaceholder: "结束日期",
placeholder: "请选择日期",
clearable: true,
type: queryForm.value.timeType,//时间类型 日 周 月 年
format: formatStr.value,//回显参数格式
"value-format": "YYYY-MM-DD",
},
},
}
{
key: "timeType",
label: "",
width: 100,
colCfg: {
span: 24,
},
renderConfig: {
type: "Select",
options: [
{
id: "daterange",
name: "日",
},
{
id: "week",
name: "周",
},
{
id: "month",
name: "月",
},
{
id: "year",
name: "年",
},
],
attributes: {
placeholder: "时间周期",
clearable: false,
},
},
}
回显日期格式
const formatStr = computed(() => {
let type = "YYYY-MM-DD";
switch (queryForm.value.timeType) {
case "daterange":
type = "YYYY-MM-DD";
break;
case "week":
type = "YYYY年 第ww周";
break;
case "month":
type = "YYYY-MM";
break;
case "year":
type = "YYYY";
break;
default:
break;
}
return type;
});
const getTableData = async () => {
await nextTick();
const params = {
...queryForm.value,
pageSize: pageSize.value,
pageNum: pageNum.value,
};
let newForm = {};
for (const key in params) {
if (Object.prototype.hasOwnProperty.call(params, key)) {
const element = params[key];
if (key === "timeArr") {
let endtime = "";
let startTime = element;
switch (params.timeType) {
case "daterange":
startTime = element[0];
endtime = element[1];
break;
case "week":
endtime = dayjs(element).endOf("week").format("YYYY-MM-DD");
break;
case "month":
endtime = dayjs(element).endOf("month").format("YYYY-MM-DD");
break;
case "year":
endtime = dayjs(element).endOf("year").format("YYYY-MM-DD");
break;
default:
break;
}
newForm["startTime"] = startTime;
newForm["endTime"] = endtime;
} else {
newForm[key] = element;
}
}
}
console.log("newForm: ", newForm);
}