配合的elementul的日期选择器使用的
参数是这样的:-1代表'上',0代表'本',1代表'下'
本月:1号到今天。意思就是比如今天是10月27号,那点击这个获取的是10月1号到10月27号得
效果如图:按顺序点了一遍,从左到右
上代码!
<template>
<div style="padding: 40px">
<el-date-picker
v-model="timerValue"
value-format="yyyy-MM-dd"
type="daterange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
style="margin-right: 20px"
>
</el-date-picker>
<div style="margin-top: 20px">
<el-button type="primary" size="mini" @click="submitTimejt(-1)"
>昨日</el-button
>
<el-button type="primary" size="mini" @click="submitTimejt(0)"
>今日</el-button
>
<el-button type="primary" size="mini" @click="submitTimejt(1)"
>明日</el-button
>
<el-button type="primary" size="mini" @click="submitTime(7)"
>最近一周(向前推)</el-button
>
<el-button type="primary" size="mini" @click="submitTime(30)"
>最近30天(向前推)</el-button
>
<el-button type="primary" size="mini" @click="getWeektime(-1)"
>上周</el-button
>
<el-button type="primary" size="mini" @click="getWeektime(0)"
>本周</el-button
>
<el-button type="primary" size="mini" @click="getWeektime(1)"
>下周</el-button
>
<el-button type="primary" size="mini" @click="submitTimeSy(-1)"
>上月(完整月)</el-button
>
<el-button type="primary" size="mini" @click="submitTimeSy(0)"
>本月(完整月)</el-button
>
<el-button type="primary" size="mini" @click="submitTimeSy(1)"
>下月(完整月)</el-button
>
<el-button type="primary" size="mini" @click="submitTimeBy"
>本月(1号到今天)</el-button
>
<el-button type="primary" size="mini" @click="checkQuartertimer(-1)"
>上季度</el-button
>
<el-button type="primary" size="mini" @click="checkQuartertimer(0)"
>本季度</el-button
>
<el-button type="primary" size="mini" @click="checkQuartertimer(1)"
>下季度</el-button
>
<el-button type="primary" size="mini" @click="getYeartimer(-1)"
>去年</el-button
>
<el-button type="primary" size="mini" @click="getYeartimer(0)"
>本年</el-button
>
<el-button type="primary" size="mini" @click="getYeartimer(1)"
>明年</el-button
>
<el-button type="primary" size="mini" @click="checkQuarter"
>当前是第几季度</el-button
>
</div>
</div>
</template>
<script>
export default {
data() {
return {
//日期
timerValue: "",
};
},
methods: {
//查看日期
log() {
console.log(this.timerValue, "日期值");
},
//【获取n天前时间】: 传参数几就是今天往前推几天的日期。比如传7就是往前七天的日期
submitTime(n) {
//开始时间
var nowDate = new Date();
let befDate = new Date(nowDate.getTime() - n * 24 * 3600 * 1000);
let byear = befDate.getFullYear();
let bmonth = befDate.getMonth() + 1;
if (bmonth >= 1 && bmonth <= 9) {
bmonth = "0" + bmonth;
}
let bday = befDate.getDate();
if (bday >= 0 && bday <= 9) {
bday = "0" + bday;
}
//截止时间
let endDate = new Date(nowDate.getTime() - 1 * 24 * 3600 * 1000);
let year = endDate.getFullYear();
let month = endDate.getMonth() + 1;
if (month >= 1 && month <= 9) {
month = "0" + month;
}
let day = endDate.getDate();
if (day >= 0 && day <= 9) {
day = "0" + day;
}
//赋值
let that = this;
that.timerValue = [
byear + "-" + bmonth + "-" + bday,
year + "-" + month + "-" + day,
];
this.log();
},
//【查询当前第几季度】
checkQuarter() {
console.log(this.getQuarterByMonth());
},
//【本月】:当前月1号到今天
submitTimeBy() {
let that = this;
var date = new Date();
var month = parseInt(date.getMonth() + 1);
var day = date.getDate();
if (month < 10) month = "0" + month;
let yuechu = date.getFullYear() + "-" + month + "-" + "01";
if (day == 1) {
that.timerValue = [yuechu, yuechu];
} else {
day = day - 1;
if (day < 10) day = "0" + day;
let yuedi = date.getFullYear() + "-" + month + "-" + day;
that.timerValue = [yuechu, yuedi];
}
that.log();
},
//-------------------------日周月季年,第一个参数:开始时间(s)结束时间(e),第二个参数:上(-1)本(0)下(1)
//【昨天,今天,明天】
submitTimejt(num) {
let value = this.getDate(num);
this.timerValue = [value, value];
this.log();
},
//【去周,本周,下周】
getWeektime(num) {
let s = this.getMonday("s", num);
let e = this.getMonday("e", num);
this.timerValue = [s, e];
this.log();
},
//【上月,本月,下月】:1号到月底
submitTimeSy(num) {
let s = this.getMonth("s", num);
let e = this.getMonth("e", num);
this.timerValue = [s, e];
this.log();
},
//【上季度,本季度,下季度】参数:-1是上季度,0是本季度,1是下季度
checkQuartertimer(num) {
if (num == -1) {
let s = this.getDayTimer(this.getLastQuarterStartDate());
let e = this.getDayTimer(this.getLastQuarterEndDate());
this.timerValue = [s, e];
this.log();
return;
} else if (num == 0) {
let s = this.getDayTimer(this.getQuarterMonth("s"));
let e = this.getDayTimer(this.getQuarterMonth("e"));
this.timerValue = [s, e];
this.log();
return;
} else if (num == 1) {
let s = this.getDayTimer(this.getNextQuarterStartDate());
let e = this.getDayTimer(this.getNextQuarterEndDate());
this.timerValue = [s, e];
this.log();
return;
}
},
//【去年,本年,明年】
getYeartimer(num) {
let s = this.getYear("s", num);
let e = this.getYear("e", num);
this.timerValue = [s, e];
this.log();
},
//日获取
getDate(dates) {
var dd = new Date();
var n = dates || 0;
dd.setDate(dd.getDate() + n);
var y = dd.getFullYear();
var m = dd.getMonth() + 1;
var d = dd.getDate();
m = m < 10 ? "0" + m : m;
d = d < 10 ? "0" + d : d;
var day = y + "-" + m + "-" + d;
return day;
},
//周获取
getMonday(type, dates) {
var now = new Date();
var nowTime = now.getTime();
var day = now.getDay();
var longTime = 24 * 60 * 60 * 1000;
var n = longTime * 7 * (dates || 0);
if (type == "s") {
var dd = nowTime - (day - 1) * longTime + n;
}
if (type == "e") {
var dd = nowTime + (7 - day) * longTime + n;
}
dd = new Date(dd);
var y = dd.getFullYear();
var m = dd.getMonth() + 1;
var d = dd.getDate();
m = m < 10 ? "0" + m : m;
d = d < 10 ? "0" + d : d;
var day = y + "-" + m + "-" + d;
return day;
},
//月度获取
getMonth(type, months) {
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
if (Math.abs(months) > 12) {
months = months % 12;
}
if (months != 0) {
if (month + months > 12) {
year++;
month = (month + months) % 12;
} else if (month + months < 1) {
year--;
month = 12 + month + months;
} else {
month = month + months;
}
}
month = month < 10 ? "0" + month : month;
var date = d.getDate();
var firstday = year + "-" + month + "-" + "01";
var lastday = "";
if (
month == "01" ||
month == "03" ||
month == "05" ||
month == "07" ||
month == "08" ||
month == "10" ||
month == "12"
) {
lastday = year + "-" + month + "-" + 31;
} else if (month == "02") {
if (
(year % 4 == 0 && year % 100 != 0) ||
(year % 100 == 0 && year % 400 == 0)
) {
lastday = year + "-" + month + "-" + 29;
} else {
lastday = year + "-" + month + "-" + 28;
}
} else {
lastday = year + "-" + month + "-" + 30;
}
var day = "";
if (type == "s") {
day = firstday;
} else {
day = lastday;
}
return day;
},
//季度获取
getQuarterMonth(type) {
var now = new Date();
var nowMonth = now.getMonth();
var year = now.getFullYear();
var quarterStartMonth = 1;
if (nowMonth < 3) {
quarterStartMonth = 1;
}
if (2 < nowMonth && nowMonth < 6) {
quarterStartMonth = 4;
}
if (5 < nowMonth && nowMonth < 9) {
quarterStartMonth = 7;
}
if (nowMonth > 8) {
quarterStartMonth = 10;
}
if (type == "s") {
var day = year + "-" + quarterStartMonth + "-01";
} //1-3 4-6 7-9 10-12
if (type == "e") {
if (quarterStartMonth == 1) {
var day = year + "-" + (quarterStartMonth + 2) + "-31";
}
if (quarterStartMonth == 10) {
var day = year + "-" + (quarterStartMonth + 2) + "-31";
}
if (quarterStartMonth == 4) {
var day = year + "-" + (quarterStartMonth + 2) + "-30";
}
if (quarterStartMonth == 7) {
var day = year + "-" + (quarterStartMonth + 2) + "-30";
}
}
return day;
},
//年度获取
getYear(type, dates) {
var dd = new Date();
var n = dates || 0;
var year = dd.getFullYear() + Number(n);
if (type == "s") {
var day = year + "-01-01";
}
if (type == "e") {
var day = year + "-12-31";
}
if (!type) {
var day = year + "-01-01/" + year + "-12-31";
}
return day;
},
//上个季度开始时间
getLastQuarterStartDate() {
var now = new Date();
var nowYear = now.getFullYear(); //当前年
return new Date(nowYear, this.getQuarterStartMonth() - 3, 1);
},
//上个季度结束时间
getLastQuarterEndDate() {
var quarterEndMonth = this.getQuarterStartMonth() - 1;
var now = new Date();
now.setMonth(quarterEndMonth);
var nowMonth = now.getMonth();
var nowYear = now.getFullYear();
return new Date(
nowYear,
quarterEndMonth,
this.getMonthDays(quarterEndMonth)
);
},
//下个季度开始时间
getNextQuarterStartDate() {
var now = new Date();
var nowYear = now.getFullYear(); //当前年
return new Date(nowYear, this.getQuarterStartMonth() + 3, 1);
},
//下个季度结束时间
getNextQuarterEndDate() {
var quarterEndMonth = this.getQuarterStartMonth() + 5;
var now = new Date();
now.setMonth(quarterEndMonth);
var nowMonth = now.getMonth();
var nowYear = now.getFullYear();
return new Date(
nowYear,
quarterEndMonth,
this.getMonthDays(quarterEndMonth)
);
},
//获得本季度的开始月份
getQuarterStartMonth() {
var nowMonth = new Date().getMonth();
var quarterStartMonth = 0;
if (nowMonth < 3) {
quarterStartMonth = 0;
}
if (2 < nowMonth && nowMonth < 6) {
quarterStartMonth = 3;
}
if (5 < nowMonth && nowMonth < 9) {
quarterStartMonth = 6;
}
if (nowMonth > 8) {
quarterStartMonth = 9;
}
return quarterStartMonth;
},
//获得某月的天数
getMonthDays(myMonth) {
var nowYear = new Date().getFullYear(); //当前年
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;
},
// 【时间格式转换 YYYY-MM-DD】
getDayTimer(data) {
var date = new Date(data);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? "0" + m : m;
var d = date.getDate();
d = d < 10 ? "0" + d : d;
var currentdate = y + "-" + m + "-" + d;
return currentdate;
},
// 【时间格式转换 YYYY-MM-DD hh:mm:ss】
getDayTimerHms(data) {
var date = new Date(data);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? "0" + m : m;
var d = date.getDate();
d = d < 10 ? "0" + d : d;
var currentdate = y + "-" + m + "-" + d;
var hh = date.getHours();
hh = hh < 10 ? "0" + hh : hh;
var mm = date.getMinutes();
mm = mm < 10 ? "0" + mm : mm;
var ss = date.getSeconds();
ss = ss < 10 ? "0" + ss : ss;
var time = hh + ":" + mm + ":" + ss;
return currentdate + " " + time;
},
// 获取当前是第几季度
getQuarterByMonth() {
var today = new Date(); //获取当前时间
var month = today.getMonth() + 1; //getMonth返回0-11
//下面是几个if
if (month >= 1 && month <= 3) {
return 1;
} else if (month >= 4 && month <= 6) {
return 2;
} else if (month >= 7 && month <= 9) {
return 3;
} else {
return 4;
}
},
},
};
</script>