fenzhi-utils
Version:
分值前端项目的js函数库
45 lines (43 loc) • 1.2 kB
JavaScript
/**
* 获取本周时间
* @param {number} n 代表要获取周几的数字
* @param {date} time 代表要获取的时间
* @returns {string} 返回获取到的周几的时间
*/
/**
CustomWeekTime(0,'2023-04-03');// 【周一 ~ 周日】 获取周一 '2023-04-03'
CustomWeekTime(-6,'2023-04-03');// 【周一 ~ 周日】 获取周日 '2023-04-09'
CustomWeekTime(1,'2023-04-03');// 【周日 ~ 周六】 获取周日 '2023-04-02'
CustomWeekTime(-5,'2023-04-03');// 【周日 ~ 周六】 获取周六 '2023-04-08'
*/
export function CustomWeekTime(n, time) {
var now = new Date(time);
var year = now.getFullYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var day = now.getDay();
if (day !== 0) {
n = n + (day - 1);
} else {
n = n + day;
}
if (day) {
if (month > 1) {
month = month;
} else {
year = year - 1;
month = 12;
}
}
now.setDate(now.getDate() - n);
year = now.getFullYear();
month = now.getMonth() + 1;
date = now.getDate();
s =
year +
'-' +
(month < 10 ? '0' + month : month) +
'-' +
(date < 10 ? '0' + date : date);
return s;
}