首页 > 其他分享 >Element 中查询前多少天、前多少周、前多少月的数据

Element 中查询前多少天、前多少周、前多少月的数据

时间:2023-02-09 12:02:15浏览次数:58  
标签:endMonth name 查询 let time Date new Element 多少


在开发后台管理系统时,经常会遇到这样一种需求,查询前多少天、多少周、多少月的数据,虽然 UI框架有自带的组件可以实现这些功能,但是操作起来却不是很方便,而且这些都是查询最近时间的数据,没有必要用日期组件,这种情况下就需要自己写了。

Element 中查询前多少天、前多少周、前多少月的数据_List

以上功能的基本实现思路为:根据日、周、月分别定义三个下拉选项,选择不同的日期类型时,显示不同的日期下拉选项,默认为第一个下拉选项。

以下是这个功能中主要用到的一些方法的代码实现:

1. 格式化时间方法

// 格式化时间
formatTime(now){
let year = now.getFullYear();
let month = now.getMonth()+1;
let date = now.getDate();
month = month<10?'0'+month:month;
date = date<10?'0'+date:date;
let time = year +'-'+month+'-'+date;
return time;
},

2. 获取日查询选项

这里仅获取前30天的下拉选项:

// 获取天的选项
getDayOptions(){
let timeList = [];
for(let i=1;i<31;i++){
let time = new Date();
time.setTime(time.getTime()-24*60*60*1000*i);
let day = this.formatTime(time);
let name ='前'+i+'天';
if(i==1){
name="昨天";
}
if(i==2){
name="前天"
}
let timeItem = {
name:name,
value:day,
};
timeList.push(timeItem);
};
this.dayArray = timeList;
},

3. 获取周查询选项

这里仅获取前8周的下拉选项:

// 获取周的选项
getWeekOptions(){
let timeList = [];
for(let i=0;i<8;i++){
let time = new Date();
let dayA = "";
let dayB = "";

// 当前星期几
let currentDay = time.getDay() || 7;
// 一星期毫秒
let weekTimestamp = 60*60*1000*24*7;

let name;


// 如果当前时间为星期一
if(currentDay == 1){

// 周起始时间
dayA = this.formatTime(new Date(
time - weekTimestamp*(i+1)
));
// 周结束时间
dayB = this.formatTime(new Date(
time - 86400000 - weekTimestamp*i
));

name = '前'+(i+1)+'周';

}else{
if(i==0){
// 本周起始时间
dayA = this.formatTime(new Date(
time-(currentDay-1)*86400000
));
// 当前时间
dayB = this.formatTime(new Date());
name = '本周';
}else{
// 周起始时间
dayA = this.formatTime(new Date(
time-(currentDay-1)*86400000 - weekTimestamp*i
));
// 周结束时间
dayB = this.formatTime(new Date(
time - currentDay*86400000 - weekTimestamp*(i-1)
));

name = '前'+i+'周';
}
}

let timeItem = {
name:name,
value:dayA+'至'+dayB
};

timeList.push(timeItem);
};

this.weekArray = timeList;

},

4. 获取月查询选项

这里仅获取前6个月的下拉选项:

// 获取月的选项
getMonthOptions(){
let timeList = [];
for(let i=0;i<6;i++){

let time = new Date();
let year = time.getFullYear();
let month = time.getMonth();
let date = time.getDate();

let dayA = "";
let dayB = "";

let name;

// 当前时间为1号
if(date==1){

let beginMonth = month-i-1;
let endMonth = month-i;

let beginYear = year;
let endYear= year;

if(beginMonth<0){
beginYear-=1;
};

if(endMonth<0){
endYear-=1;
};


if(beginMonth<0){
beginMonth = 12 + beginMonth;
};
if(endMonth<0){
endMonth = 12 + endMonth;
};


beginMonth=beginMonth<10?'0'+beginMonth:beginMonth;
endMonth=endMonth<10?'0'+endMonth:endMonth;


let A = new Date(beginYear,beginMonth,'01') - 0;
let B = new Date(endYear,endMonth,'01') - 86400000;

dayA = this.formatTime(new Date(A));
dayB = this.formatTime(new Date(B));

name= '前'+(i+1)+'月';

}else{

if(i==0){

dayA = this.formatTime(new Date(year,month,1));
dayB = this.formatTime(new Date());

name= '本月';

}else{


let beginMonth = month-i;
let endMonth = month-i+1;

let beginYear = year;
let endYear= year;

if(beginMonth<0){
beginYear-=1;
};

if(endMonth<0){
endYear-=1;
};


if(beginMonth<0){
beginMonth = 12 + beginMonth;
};
if(endMonth<0){
endMonth = 12 + endMonth;
};

beginMonth=beginMonth<10?'0'+beginMonth:beginMonth;
endMonth=endMonth<10?'0'+endMonth:endMonth;

let A = new Date(beginYear,beginMonth,'01') - 0;
let B = new Date(endYear,endMonth,'01') - 86400000;

dayA = this.formatTime(new Date(A));
dayB = this.formatTime(new Date(B));

name= '前'+ i +'月';
}
};


let timeItem = {
name:name,
value:dayA+'至'+dayB
};

timeList.push(timeItem);
};

this.monthArray = timeList;

},

5. 完整的代码参考

<template>

<div>
<el-radio-group v-model="queryParam.type" style="margin-right:10px;" @change="onTimeChange">
<el-radio-button label="day">日榜</el-radio-button>
<el-radio-button label="week">周榜</el-radio-button>
<el-radio-button label="month">月榜</el-radio-button>
</el-radio-group>

<el-select v-model="day_time" v-if="queryParam.type=='day'" @change="onTimeChange">
<el-option
v-for="item in dayArray"
:key="item.value"
:label="item.value"
:value="item.value">
</el-option>
</el-select>

<el-select v-model="week_time" v-if="queryParam.type=='week'" @change="onTimeChange">
<el-option
v-for="item in weekArray"
:key="item.value"
:label="item.value"
:value="item.value">
</el-option>
</el-select>

<el-select v-model="month_time" v-if="queryParam.type=='month'" @change="onTimeChange">
<el-option
v-for="item in monthArray"
:key="item.value"
:label="item.value"
:value="item.value">
</el-option>
</el-select>



</div>


</template>

<script>


export default {
name: "index",
data() {
return {

// 选择的日
day_time:"",
// 选择的周
week_time:"",
// 选择的月
month_time:"",


// 日查询选项
dayArray:[],
// 周查询选项
weekArray:[],
// 月查询选项
monthArray:[],

// 查询的参数
queryParam:{
type:"week",
end_time:""
begin_time:"",
}

};
},

created(){

this.getDayOptions();
this.getWeekOptions();
this.getMonthOptions();
this.setCurrentDate();

},




methods: {

// 格式化时间
formatTime(now){
let year = now.getFullYear();
let month = now.getMonth()+1;
let date = now.getDate();
month = month<10?'0'+month:month;
date = date<10?'0'+date:date;
let time = year +'-'+month+'-'+date;
return time;
},


// 获取天的选项
getDayOptions(){
let timeList = [];
for(let i=1;i<31;i++){
let time = new Date();
time.setTime(time.getTime()-24*60*60*1000*i);
let day = this.formatTime(time);
let name ='前'+i+'天';
if(i==1){
name="昨天";
}
if(i==2){
name="前天"
}
let timeItem = {
name:name,
value:day,
};
timeList.push(timeItem);
};
this.dayArray = timeList;
},

// 获取周的选项
getWeekOptions(){
let timeList = [];
for(let i=0;i<8;i++){
let time = new Date();
let dayA = "";
let dayB = "";

// 当前星期几
let currentDay = time.getDay() || 7;
// 一星期毫秒
let weekTimestamp = 60*60*1000*24*7;

let name;


// 如果当前时间为星期一
if(currentDay == 1){

// 周起始时间
dayA = this.formatTime(new Date(
time - weekTimestamp*(i+1)
));
// 周结束时间
dayB = this.formatTime(new Date(
time - 86400000 - weekTimestamp*i
));

name = '前'+(i+1)+'周';

}else{
if(i==0){
// 本周起始时间
dayA = this.formatTime(new Date(
time-(currentDay-1)*86400000
));
// 当前时间
dayB = this.formatTime(new Date());
name = '本周';
}else{
// 周起始时间
dayA = this.formatTime(new Date(
time-(currentDay-1)*86400000 - weekTimestamp*i
));
// 周结束时间
dayB = this.formatTime(new Date(
time - currentDay*86400000 - weekTimestamp*(i-1)
));

name = '前'+i+'周';
}
}

let timeItem = {
name:name,
value:dayA+'至'+dayB
};

timeList.push(timeItem);
};

this.weekArray = timeList;

},

// 获取月的选项
getMonthOptions(){
let timeList = [];
for(let i=0;i<6;i++){

let time = new Date();
let year = time.getFullYear();
let month = time.getMonth();
let date = time.getDate();

let dayA = "";
let dayB = "";

let name;

// 当前时间为1号
if(date==1){

let beginMonth = month-i-1;
let endMonth = month-i;

let beginYear = year;
let endYear= year;

if(beginMonth<0){
beginYear-=1;
};

if(endMonth<0){
endYear-=1;
};


if(beginMonth<0){
beginMonth = 12 + beginMonth;
};
if(endMonth<0){
endMonth = 12 + endMonth;
};


beginMonth=beginMonth<10?'0'+beginMonth:beginMonth;
endMonth=endMonth<10?'0'+endMonth:endMonth;


let A = new Date(beginYear,beginMonth,'01') - 0;
let B = new Date(endYear,endMonth,'01') - 86400000;

dayA = this.formatTime(new Date(A));
dayB = this.formatTime(new Date(B));

name= '前'+(i+1)+'月';

}else{

if(i==0){

dayA = this.formatTime(new Date(year,month,1));
dayB = this.formatTime(new Date());

name= '本月';

}else{


let beginMonth = month-i;
let endMonth = month-i+1;

let beginYear = year;
let endYear= year;

if(beginMonth<0){
beginYear-=1;
};

if(endMonth<0){
endYear-=1;
};


if(beginMonth<0){
beginMonth = 12 + beginMonth;
};
if(endMonth<0){
endMonth = 12 + endMonth;
};

beginMonth=beginMonth<10?'0'+beginMonth:beginMonth;
endMonth=endMonth<10?'0'+endMonth:endMonth;

let A = new Date(beginYear,beginMonth,'01') - 0;
let B = new Date(endYear,endMonth,'01') - 86400000;

dayA = this.formatTime(new Date(A));
dayB = this.formatTime(new Date(B));

name= '前'+ i +'月';
}
};


let timeItem = {
name:name,
value:dayA+'至'+dayB
};

timeList.push(timeItem);
};

this.monthArray = timeList;

},



// 设置当前时间
setCurrentDate(){


let type = this.queryParam.type;

let defaultDay = this.dayArray[0];
let defaultWeek = this.weekArray[0];
let defaultMonth = this.monthArray[0];

if(type=="day"){

if(this.day_time==""){
let current = defaultDay.value;
this.day_time = current;
};

this.queryParam.begin_time = this.day_time ;
this.queryParam.end_time = this.day_time ;

};



if(type == "week"){

if(this.week_time==""){
let current = defaultWeek.value;
this.week_time = current;
};

let arr = this.week_time.split("至");
this.queryParam.begin_time = arr[0];
this.queryParam.end_time = arr[1];

};


if(type=="month"){
if(this.month_time==""){
let current = defaultMonth.value;
this.month_time = current;
};

let arr = this.month_time.split("至");
this.queryParam.begin_time = arr[0];
this.queryParam.end_time = arr[1];
};


},


// 当时间改变时
onTimeChange(){
this.setCurrentDate();

// 根据时间获取数据
// this.getTableData();
},


},
};
</script>

标签:endMonth,name,查询,let,time,Date,new,Element,多少
From: https://blog.51cto.com/u_15959833/6046620

相关文章

  • MySQL查询一年、一周、三个月的数据
    阅读目录DATE_SUB()函数从日期减去指定的时间间隔DATE_SUB()函数从日期减去指定的时间间隔语法:DATE_SUB(date,INTERVALexprtype)date参数是合法的日期表达......
  • MySQL查询某天的数据
    阅读目录1、DATE_FORMAT()函数用于以不同的格式显示日期/时间数据2、DATEDIFF()函数返回两个日期之间的天数扩展知识1、DATE_FORMAT()函数用于以不同的格式......
  • MySQL给查询加序号
    阅读目录数据表MySQL给查询加序号解释说明相关知识点数据表DROPTABLEIFEXISTStb_score;CREATETABLEtb_score(idINT(11)NOTNULLauto_incr......
  • Vue2.9.6安装element-ui
    阅读目录安装element-ui源码路由文件:E:\node\vue296\src\router\index.js入口:E:\node\vue296\src\main.js组件:E:\node\vue296\src\components\Count.vue......
  • ElementUI手动控制popover弹层的显示与隐藏
    转自于:https://huaweicloud.csdn.net/638f116ddacf622b8df8e566.html?spm=1001.2101.3001.6650.9&utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogComme......
  • 你的企业IM安全吗?对于私有化的即时通讯软件你了解多少?
    企业IM是指企业即时通讯,与微信、QQ等我们常用的个人通讯软件不同,它是供企业内部使用的即时交流与沟通工具。企业员工可以通过安装了即时通讯软件,进行两人以及多人之间的实时......
  • 线段树查询i到j最长增加子串和序列
    基础篇最长增加子数组-楠030416-博客园(cnblogs.com)增加线段树子串#include<bits/stdc++.h>usingnamespacestd;//最长连续增加子串inta[100],dp[100],tr......
  • 230. Kth Smallest Element in a BST[Medium]
    230.KthSmallestElementinaBSTGiventherootofabinarysearchtree,andanintegerk,returnthekthsmallestvalue(1-indexed)ofallthevaluesofthe......
  • 开放API 与 查询语言GraphQL
    1、开放API什么是API?API(ApplicationProgrammingInterface,应用程序接口)是一些预先定义的接口(如函数、HTTP接口),或指软件系统不同组成部分衔接的约定。用来提供应用程序与开......
  • vue3+element表格数据导出
    实现效果导出后的效果:步骤第一步:安装依赖npminstall--savexlsxfile-saver第二步:给表格添加id,导出的时候需要用到<!--导出按钮--><el-buttontype="pri......