- 手动获取该日期所在的周中的周一日期和周日日期
function getWeekDays (inputDay:any) {
const oneDayTime = 1000 * 60 * 60 * 24;
const today = new Date(inputDay);
// 若那一天是周末时,则强制赋值为7
const todayDay = today.getDay() || 7;
const startDate = new Date(
today.getTime() - oneDayTime * (todayDay - 1)
);
const dateList = [];
const start = new Date(startDate.getTime());
const end = new Date(startDate.getTime() + 6 * oneDayTime);
dateList.push(start);
dateList.push(end);
return dateList;
}
- 为了显示需要,把一位数字转换成两位
function addZero (t: number) {
return t < 10 ? '0' + t : t;
}
- 将国际标准时间转换为yyyy-mm-dd格式
function changeDate (date:any) {
const month = date.getMonth() + 1;
const day = date.getDate();
return date.getFullYear() + '-' + addZero(month) + '-' + addZero(day);
}
- 将国际标准时间转换为 yyyy-mm-dd HH:MM 格式
function changeDate2Min (date:any) {
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const min = date.getMinutes();
return date.getFullYear() + '-' + addZero(month) + '-' + addZero(day) + ' ' + addZero(hour) + ':' + addZero(min);
}
- 获取周一到周日的日期
function weekMonth(currentTime:any) {
const currentDate = new Date(currentTime);
const timesStamp = currentDate.getTime();
const currenDay = currentDate.getDay();
const dates = [];
for (let i = 0; i < 7; i++) {
const date = new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)).toLocaleDateString().replace(/\//g, '-');
const dateList = date.split('-');
Number(dateList[1]) >= 10 ? dateList[1] = '' + dateList[1] : dateList[1] = '0' + dateList[1];
Number(dateList[2]) >= 10 ? dateList[2] = '' + dateList[2] : dateList[2] = '0' + dateList[2];
const dataStr = dateList.join('-');
dates.push(dataStr);
}
return dates;
}
- 返回当前日的前某某天, days 想要往前推多少天,yyyy-MM-dd 格式的日期
function getDayBeforeYmd(days: number) :string {
const curDate = new Date();
const newDate = new Date(curDate.getFullYear(), curDate.getMonth(), curDate.getDate() - days);
return changeDate(newDate);
}
- 返回符合条件的星期日期
function getMonthWeek(year:any, month:any) {
// 该月最后一天
const lastDay = new Date(year, month, 0).getDate();
// 该月有几周
const weekNum = Math.ceil(lastDay / 7);
return { weekNum, lastDay };
}
function getMondayTime(year:any, month:any, weekNum:any) {
const date = new Date();
// 该月第一天
date.setFullYear(year, month - 1, 1);
// 第一天是星期几
let monthFirstWeek = date.getDay();
// 如果等于0代表是周日
if (monthFirstWeek === 0) monthFirstWeek = 7;
// 第一个满足条件的日期
let firstWeekDate;
if (monthFirstWeek < weekNum) {
firstWeekDate = weekNum - monthFirstWeek + 1;
} else if (monthFirstWeek > weekNum) {
firstWeekDate = 7 - monthFirstWeek + weekNum + 1;
} else {
firstWeekDate = 1;
}
return firstWeekDate;
}
function getTimeFunc(year:any, month:any, weekNum:any) {
const weekObj = getMonthWeek(year, month);
// 获得该月几周
const weeks = weekObj.weekNum;
const lastDay = weekObj.lastDay;
// 获取月第一个想要拿到星期的日期
const oneDate = getMondayTime(year, month, weekNum);
const weeksAll = []; // 所有符合条件的星期
for (let i = 0; i < weeks; i++) {
const date = addZero(oneDate + i * 7);
if (date <= lastDay) weeksAll.push(year + '-' + addZero(month) + '-' + date);
}
return weeksAll;
}