vue获取近七天的起始日和结束日
data() {
return {
startDate: null,
endDate: null
}
},
mounted() {
this.calculateDateRange();
},
methods: {
calculateDateRange() {
var currentDate = new Date();
var startDate = new Date();
startDate.setDate(currentDate.getDate() - 6);
this.startDate = this.formatDate(startDate);
this.endDate = this.formatDate(currentDate);
console.log(this.startDate,'2023-07-29')
console.log(this.startDate,'2023-08-04')
},
formatDate(date) {
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
return year + "-" + month + "-" + day;
}
}
vue根据月份获取起始日和结束日
data() {
return {
selectedMonth: '', // 已选择的月份
startDate: null, // 起始日
endDate: null // 结束日
}
},
watch: {
selectedMonth: function(newMonth) {
if (newMonth) {
this.calculateDateRange(newMonth);
}
}
},
methods: {
calculateDateRange(selectedMonth) {
// 将选定的月份转换为Date对象
var monthDate = new Date(selectedMonth);
var year = monthDate.getFullYear();
var month = monthDate.getMonth();
// 计算起始日和结束日
var startDay = new Date(year, month, 1);
var endDay = new Date(year, month + 1, 0);
// 格式化日期
this.startDate = this.formatDate(startDay);
this.endDate = this.formatDate(endDay);
console.log(this.startDate,'2023-01-01')
console.log(this.startDate,'2023-01-31')
},
formatDate(date) {
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
return year + "-" + month + "-" + day;
}
}
vue根据年份获取起始日和结束日
data() {
return {
selectedYear: '', // 已选择的年份
startDate: null, // 起始日
endDate: null // 结束日
}
},
watch: {
selectedYear: function(newYear) {
if (newYear) {
this.calculateDateRange(newYear);
}
}
},
methods: {
calculateDateRange(selectedYear) {
var startDate = new Date(selectedYear, 0, 1); // 选择年份的第一天
var endDate = new Date(selectedYear, 11, 31); // 选择年份的最后一天
// 格式化日期
this.startDate = this.formatDate(startDate);
this.endDate = this.formatDate(endDate);
console.log(this.startDate,'2023-01-01')
console.log(this.startDate,'2023-12-31')
},
formatDate(date) {
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
return year + "-" + month + "-" + day;
}
}