js获取昨天、今天、当周、一周内、当月、一月内的起始时间
程序员文章站
2022-05-24 14:35:39
...
代码
<template>
<div>
<span>当前周期:</span>
<span style="color: #09BA08">{{startTime+'~'+endTime}}</span>
<div>
<el-radio-group fill="#07C160" v-model="radio" size="small" @change="changePeriod">
<el-radio-button label="昨天"></el-radio-button>
<el-radio-button label="今天"></el-radio-button>
<el-radio-button label="当周"></el-radio-button>
<el-radio-button label="当月"></el-radio-button>
<el-radio-button label="一周内"></el-radio-button>
<el-radio-button label="一月内"></el-radio-button>
</el-radio-group>
</div>
</div>
</template>
<script>
export default {
name: "patrolPlan",
data(){
return{
radio:'当周',
startTime:'',
endTime:'',
}
},
methods:{
//切换昨天、今天、当周、一周内、当月、一月内
changePeriod(val){
// console.log(val);
var type="0";
switch (val) {
case '昨天':this.periodStartTime=(addDate(-1)+" 00:00:00");this.periodEndTime=(addDate(-1)+" 23:59:59");break;
case '今天':this.periodStartTime=(addDate(0)+" 00:00:00");this.periodEndTime=(addDate(0)+" 23:59:59");break;
case '当周':this.periodStartTime=(addDate(-new Date().getDay()+1)+" 00:00:00");this.periodEndTime=(addDate(7-new Date().getDay())+" 23:59:59");break;
case '一周内':this.periodStartTime=(addDate(-6)+" 00:00:00");this.periodEndTime=(addDate(0)+" 23:59:59");break;
case '当月':this.periodStartTime=(addDate(-new Date().getDate()+1)+" 00:00:00");this.periodEndTime=(addDate(mGetDate()-new Date().getDate())+" 23:59:59");break;
case '一月内':this.periodStartTime=(addDate(-30)+" 00:00:00");this.periodEndTime=(addDate(0)+" 23:59:59");break;
default:break;
}
//获取当前月份的天数
function mGetDate(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var d = new Date(year, month, 0);
return d.getDate();
}
//加减天数(含时分秒)
function addDate(days,type) {
var d = new Date();
d.setDate(d.getDate() + days);
var month = d.getMonth() + 1;
var day = d.getDate();
var hour = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
if (month < 10) {
month = "0" + month;
}
if (day < 10) {
day = "0" + day;
}
var time = "";
//type不是空字符串 表示需要时分秒
if (type != null && "" != type) {
if (hour < 10) {
hour = "0" + hour;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
time = " " + hour + ":" + minutes + ":" + seconds;
}
var val = d.getFullYear() + "-" + month + "-" + day + time;
return val;
}
this.getPatrolStatistics(this.periodStartTime,this.periodEndTime);
},
}
}
</script>
上一篇: 获取cookie 设置cookie
下一篇: cookie设置