这里做个记录
<template>
<div class="project-home">
<button @click="reduceDate">前一天</button>
<p>{{date}}</p>
<button @click="addDate">后一天</button>
<br>
<button @click="reduceYear">前一月</button>
<p>{{month}}</p>
<button @click="addYear">后一月</button>
<br>
<button @click="getaddweek">前一周</button>
<p>{{weekstart}}-{{weekend}}</p>
<button @click="getalastweek">后一周</button>
</div>
</template>
<script>
export default {
name: "Project-module-home",
data(){
return{
date:"暂无当前日期",
month:"暂无月",
weekstart:"暂无开始",
weekend:"暂无结束",
}
},
created(){
this.getDate()
},
methods:{
getDate(){
var mydate = new Date();
this.date=mydate.getFullYear()+"."+(mydate.getMonth()+1)+"."+mydate.getDate();
this.month=mydate.getFullYear()+"."+(mydate.getMonth()+1);
this.getWeek(mydate.getDay());
},
//获取当前周星期日
getWeek(value){
let weekxq=value;
if(weekxq==0){
this.weekstart=this.date;
console.log(this.weekstart)
}else{
let weeka=new Date(this.date);
let weeks=weeka.getDate()-weekxq;
weeka.setDate(weeks);
this.weekstart=this.formatDate(weeka,0);
}
this.getWeekarry(this.weekstart)
},
//************************************************
//获取前一周和后一周
//************************************************
//获取一周所有时间
getWeekarry(values){
let ag=new Date(values);
//console.log(new Date(values))
let ac=null;
let weekaRrry=[];
for(let i=0;i<7;i++){
if(i==0){
ac=values;
}else{
ag.setDate(ag.getDate()+1);
ac=this.formatDate(ag,0);
}
weekaRrry.push(ac);
}
this.weekend=weekaRrry[6];
//console.log(weekaRrry)
},
//获取上一周开始时间和结束时间
getaddweek(){
console.log(this.weekstart)
this.getWeekdate("1")
},
//获取下一周开始时间和结束时间
getalastweek(){
console.log(this.weekstart)
this.getWeekdate("2")
},
getWeekdate(type){
let weeks=new Date(this.weekstart);
let weekd=new Date(this.weekstart);
let ag=null;
switch (type){
case "1":
let weekls=weeks.getDate()-7;
weekd.setDate(weekls);
ag=this.formatDate(weekd,0);
this.weekstart=ag;
this.getWeekarry(this.weekstart);//获取这周最后一天
ag=null;
break;
case "2":
let weekl=weeks.getDate()+7;
weekd.setDate(weekl);
ag=this.formatDate(weekd,0);
this.weekstart=ag;
this.getWeekarry(this.weekstart);//获取这周最后一天
ag=null;
break;
default:
break;
}
},
//************************************************
//获取前一天和后一天
//************************************************
//后一天
addDate(){
let dates=new Date(this.date);
this.getDays(dates,'1');
},
//前一天
reduceDate(){
let dates=new Date(this.date);
this.getDays(dates,'2');
},
getDays(value,type){
switch (type){
case "1":
let getDatesa=value.getDate()+1;
value.setDate(getDatesa);
this.date=this.formatDate(value,0);
break;
case "2":
let getDates=value.getDate()-1;
value.setDate(getDates);
this.date=this.formatDate(value,0);
break;
//上一个月
case "3":
let getMonths=value.getMonth()-1;
value.setMonth(getMonths);
this.month=this.formatDate(value,1)
break;
//下一个月
case "4":
let getMonthsf=value.getMonth()+1;
value.setMonth(getMonthsf);
this.month=this.formatDate(value,1)
break;
default:
break;
}
},
//************************************************
//获取上一个月和下一个月
//************************************************
//上一个月
reduceYear(){
let months=new Date(this.month);
this.getDays(months,'3')
},
//下一个月
addYear(){
let months=new Date(this.month);
this.getDays(months,'4')
},
formatDate(date,i){
var year = date.getFullYear().toString();
var month = (date.getMonth()+1).toString();
var day = date.getDate().toString();
if(month<10){
month="0"+month;
}
if(day<10){
day="0"+day;
}
switch (i){
case 0:
return year+'.'+month+'.'+day;
break;
case 1:
return year+'.'+month;
break;
}
},
}
}
</script>
<style scoped>
</style>