<template>
<div>{{ nowDate }}</div>
</template>
<script>
export default {
data() {
return {
nowDate: "", // 当前日期
year: "",
month: "",
day: ""
};
},
methods: {
// 正常的是月份+1
formatDate() {
let date = new Date();
this.month = date.getMonth()+1; //月
this.day = date.getDate();//日
/*
/^\d$/.test(date.getDate())
? "0" + date.getDate()
: date.getDate();
这个是不满10前面自动补0
*/
this.year = date.getFullYear(); //年
// 这个是更改月份 当天数小于3号时 月份-2 PS正常是加1 不加为-1
// 反则月份-1显示
// if (this.day < 3) {
// this.month = (date.setMonth(date.getMonth() - 1)) ;//这个是改变日期的变化
// this.month=date.getMonth()
// } else {
// this.month = date.getMonth();
// }
this.nowDate =`${this.year}/${this.month}/${this.day}`;
}
},
mounted() {
this.formatDate();
}
};
</script>
不满10前面自动补0
formatTime() {
let date = new Date();
// this.month = date.getMonth(); //月
this.month = /^\d$/.test(date.getMonth())
? "0" + date.getMonth()
: date.getMonth(); //月
// this.day = date.getDate();
this.day = /^\d$/.test(date.getDate())
? "0" + date.getDate()
: date.getDate();
this.year = date.getFullYear(); //年
if (this.day < 3) {
this.month = date.setMonth(date.getMonth() - 1);
this.month = /^\d$/.test(date.getMonth())
? "0" + date.getMonth()
: date.getMonth();
} else {
this.month = /^\d$/.test(date.getMonth())
? "0" + date.getMonth()
: date.getMonth();
}
this.nowdata = Number(`${this.year}${this.month}${this.day}`);
},
Vue 获取当下日期时间及周几
https://www.jianshu.com/p/023623d0fa6a
判断如果周2当前日期-2 否则正常
!vue!<template>
<div>
<p>{{nowWeek}}</p>
<p>
<span>{{nowDate}}</span>
</p>
</div>
</template>
<script>
export default {
data() {
return {
nowWeek: "",
nowDate: "",
};
},
mounted() {
this.setNowTimes();
},
methods:{
setNowTimes () {
let myDate = new Date()
let wk = myDate.getDay()
let yy = String(myDate.getFullYear())
let mm = myDate.getMonth() + 1
let dd = String(myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate())
let hou = String(myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours())
let min = String(myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes())
let sec = String(myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds())
let weeks = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
let week = weeks[wk]
if(week ==='星期二'){
dd = myDate.setDate(myDate.getDate() - 2);
dd= /^\d$/.test(myDate.getDate())
? "0" + myDate.getDate()
:myDate.getDate();
}else{
dd = /^\d$/.test(myDate.getDate())
? "0" +myDate.getDate()
: myDate.getDate();
}
this.nowDate = yy + '-' + mm + '-' + dd
this.nowWeek = week
}
}
};
</script>
<style>
</style>