js获取今天、昨天、近三天、本周、上周、本月、上月、本季度、上季度、本年、上年、
getDateTypeValue(type) {
let d = new Date();
let startDate = ''
let endDate = ''
// 今天
if (type == 0) {
startDate = this.getYYYYMMDD(d)
endDate = startDate;
} else if (type == 1) {
// 昨天
d.setDate(d.getDate() - 1);
startDate = this.getYYYYMMDD(d)
endDate = startDate;
} else if (type == 2) {
// 近三天
endDate = this.getYYYYMMDD(d)
d.setDate(d.getDate() - 2);
startDate = this.getYYYYMMDD(d)
} else if (type == 3) {
// 本周
const week = d.getDay()
//一天的毫秒数
const millisecond = 1000 * 60 * 60 * 24
//减去的天数
const minusDay = week != 0 ? week - 1 : 6
//本周 周一
const monday = new Date(d.getTime() - minusDay * millisecond)
//本周 周日
const sunday = new Date(monday.getTime() + 6 * millisecond)
startDate = this.getYYYYMMDD(monday)
endDate = this.getYYYYMMDD(sunday)
} else if (type == 4) {
// 上周
let weekNum = d.getDay()
weekNum = weekNum == 0 ? 7 : weekNum
let lastDate = new Date(d.getTime() - weekNum * 24 * 60 * 60 * 1000)
let firstDate = new Date(d.getTime() - (weekNum + 6) * 24 * 60 * 60 * 1000)
startDate = this.getYYYYMMDD(firstDate)
endDate = this.getYYYYMMDD(lastDate)
} else if (type == 5) {
// 近7天
endDate = this.getYYYYMMDD(d)
d.setDate(d.getDate() - 6);
startDate = this.getYYYYMMDD(d)
} else if (type == 6) {
// 近14天
endDate = this.getYYYYMMDD(d)
d.setDate(d.getDate() - 13);
startDate = this.getYYYYMMDD(d)
} else if (type == 7) {
// 本月
let s1 = new Date(d.getFullYear(), d.getMonth()+1, 0)
let s2 = new Date(d.getFullYear(), d.getMonth(), 1)
endDate = this.getYYYYMMDD(s1)
startDate = this.getYYYYMMDD(s2)
} else if (type == 8) {
// 上月
let s1 = new Date(d.getFullYear(), d.getMonth(), 0)
let s2 = new Date(d.getFullYear(), d.getMonth()-1, 1)
endDate = this.getYYYYMMDD(s1)
startDate = this.getYYYYMMDD(s2)
} else if (type == 9) {
// 本季
let month = d.getMonth() + 1;//getMonth返回0-11
let year = d.getFullYear();
if(month >=1 && month <=3){
startDate = this.getYYYYMMDD(new Date(year, 0, 1));
endDate = this.getYYYYMMDD(new Date(year, 3, 0));
}else if(month >=4 && month <=6){
startDate = this.getYYYYMMDD(new Date(year, 3, 1));
endDate = this.getYYYYMMDD(new Date(year, 6, 0));
}else if(month >=7 && month <=9){
startDate = this.getYYYYMMDD(new Date(year, 6, 1));
endDate = this.getYYYYMMDD(new Date(year, 9, 0));
}else{
startDate = this.getYYYYMMDD(new Date(year, 9, 1));
endDate = this.getYYYYMMDD(new Date(year, 12, 0));
}
} else if (type == 10) {
// 上季
let month = d.getMonth() + 1;//getMonth返回0-11
let year = d.getFullYear();
if(month >=1 && month <=3){
startDate = this.getYYYYMMDD(new Date(year-1, 9, 1));
endDate = this.getYYYYMMDD(new Date(year-1, 12, 0));
}else if(month >=4 && month <=6){
startDate = this.getYYYYMMDD(new Date(year, 0, 1));
endDate = this.getYYYYMMDD(new Date(year, 3, 0));
}else if(month >=7 && month <=9){
startDate = this.getYYYYMMDD(new Date(year, 3, 1));
endDate = this.getYYYYMMDD(new Date(year, 6, 0));
}else{
startDate = this.getYYYYMMDD(new Date(year, 6, 1));
endDate = this.getYYYYMMDD(new Date(year, 9, 0));
}
} else if (type == 11) {
// 本年
let year = d.getFullYear();
startDate = this.getYYYYMMDD(new Date(year, 0, 1));
endDate = this.getYYYYMMDD(new Date(year, 12, 0));
} else if (type == 12) {
// 上年
let year = d.getFullYear();
startDate = this.getYYYYMMDD(new Date(year-1, 0, 1));
endDate = this.getYYYYMMDD(new Date(year-1, 12, 0));
}
this.queryForm.startDateStr = startDate
this.queryForm.endDateStr = endDate
},
getYYYYMMDD(d) {
return d.getFullYear() + '-' + (d.getMonth() + 1).toString().padStart(2, '0') + '-' + d.getDate().toString().padStart(2, '0')
},
最后
以上就是花痴小蝴蝶最近收集整理的关于js获取今天、昨天、近三天、本周、上周、本月、上月、本季度、上季度、本年、上年、的全部内容,更多相关js获取今天、昨天、近三天、本周、上周、本月、上月、本季度、上季度、本年、上年、内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复