获取当前日期时间,获取昨天日期,获取上周日期,获取上月日期,获取
2020-11-12 本文已影响0人
蔚完待旭
Vue中
data() {
return {
/*当前日期时间*/
nowYear: '',
nowMonth: '',
nowDate: '',
nowDay: '',
nowHours: '',
nowMinutes: '',
nowSeconds: '',
turnoverTime: ''
};
},
获取当前日期时间
// 获取当前日期时间
getNowTime() {
this.nowYear = new Date().getFullYear();
this.nowMonth = new Date().getMonth()+1;
this.nowDate = new Date().getDate();
this.nowDay = new Date().getDay();
this.nowHours = new Date().getHours();
this.nowMinutes = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
this.nowSeconds = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
this.turnoverTime = this.nowYear+'年'+this.nowMonth+'月'+this.nowDate+'日'+' '+this.nowHours+':'+this.nowMinutes+':'+this.nowSeconds;
},
获取上周日期
let thisWeekStart; //本周周一的时间
if (this.nowDay === 0) { //周天的情况;
thisWeekStart = (new Date(this.nowYear + '-' + this.nowMonth + '-' + this.nowDate)).getTime() - ((this.nowDay) + 6) * 86400000;
} else {
thisWeekStart = (new Date(this.nowYear + '-' + this.nowMonth + '-' + this.nowDate)).getTime() - ((this.nowDay) - 1) * 86400000;
}
//获得上周时间
var prevWeekStart = thisWeekStart - 7 * 86400000;//上周周一的时间
var prevWeekEnd = thisWeekStart - 1 * 86400000;//上周周日的时间
let sTime = this.formatDate(new Date(prevWeekStart)); //开始时间
let eTime = this.formatDate(new Date(prevWeekEnd)); //结束时间
获取上月日期
var prevCurrentYear = 0, prevCurrentMonth = 0;
if (this.nowMonth === 1) {
prevCurrentYear = this.nowYear - 1;
prevCurrentMonth = 12;
} else {
prevCurrentYear = this.nowYear;
prevCurrentMonth = this.nowMonth - 2;
}
//获得上月时间
var prevmonthLastday = (new Date(this.nowYear, this.nowMonth-1, 1)).getTime() - 86400000;
let sTime = this.formatDate(new Date(prevCurrentYear, prevCurrentMonth, 1)); //开始时间
let eTime = this.formatDate(new Date(prevmonthLastday)); //结束时间
获取昨天日期
let date1 = new Date();
let date2 = new Date(date1);
date2.setDate(date1.getDate() - 1);
`${date2.getFullYear()}-${date2.getMonth() + 1<10?`0${date2.getMonth() + 1}`:date2.getMonth() + 1}-${date2.getDate()}`;
获取30天前日期
let date1 = new Date();
let date2 = new Date(date1);
date2.setDate(date1.getDate() - 30);
`${date2.getFullYear()}-${date2.getMonth() + 1<10?`0${date2.getMonth() + 1}`:date2.getMonth() + 1}-${date2.getDate()}`;