获取当前月的天数
getNowMonthDay(month) = > {
let date = new Date();
let year = date.getFullYear();
let days = new Date(year, month, 0).getDate();
console.log(days)
},
获取指定年份的周数并格式化 ['1周(1.1-1.7)' , '2周(1.8-1.14)' , '3周(1.15-1.21)' .... ]
getFormattedWeeksInYear(year)=> {
const firstDayOfYear = new Date(year, 0, 1);
const daysToFirstThursday = (4 - firstDayOfYear.getDay() + 7) % 7;
const firstThursday = new Date(year, 0, 1 + daysToFirstThursday);
const weeksInYear =
Math.ceil(
(new Date(year, 11, 31) - firstThursday) / (7 * 24 * 60 * 60 * 1000)
) + 1;
// 格式化展示周数数组
let weeks = [];
for (let week = 1; week <= weeksInYear; week++) {
const start = this.getDateOfISOWeek(year, week);
const end = this.getDateOfISOWeek(year, week, 6);
const formattedWeek = `${week}周 (${start.getMonth() + 1}.${start.getDate()} - ${end.getMonth() + 1}.${end.getDate()})`;
weeks.push(formattedWeek);
}
console.log(weeks)
weeks = [];
};
获取指定年份和周数的日期
getDateOfISOWeek(year, week, day = 0) =>{
const date = new Date(year, 0, 1 + (week - 1) * 7);
const dayOffset = date.getDay() - 1;
return new Date(date.setDate(date.getDate() - dayOffset + day));
},
判断今天属于哪一周
getWeekNumber() =>{
const today = new Date();
const formattedToday = `${today.getMonth() + 1}.${today.getDate()}`;
for (let i = 0; i < this.weeks.length; i++) {
const weekRange = this.weeks[i].match(/\d+\.\d+/g);
const start = weekRange[0];
const end = weekRange[1];
if (formattedToday >= start && formattedToday <= end) {
this.active = i;
}
}
return null; // 今天不属于任何一周
}
时间戳转换成yy-mm-dd
formatDate(timestamp) {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
return `${year}-${month}-${day}`;
},