获取本周日期小程序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const  current_date = date.getDate();
// 获取本 日
const current_month = date.getMonth() + 1;
// 获取本 月
const current_year = date.getFullYear();
// 获取本 年

function getWeekDay(dateString) {
let dateStringReg = /^\d{4}[/-]\d{1,2}[/-]\d{1,2}$/;

if (dateString.match(dateStringReg)) {
let presentDate = new Date(dateString),
today = presentDate.getDay() !== 0 ? presentDate.getDay() : 7;

return Array.from(new Array(7), function(val, index) {
return formatDate(new Date(presentDate.getTime() - (today - index-1) * 24 * 60 * 60 * 1000));
});

} else {
throw new Error('dateString should be like "yyyy-mm-dd" or "yyyy/mm/dd"');
}

function formatDate(date) {
return (date.getMonth() + 1) + '-' + date.getDate();
}
}

getWeekDay(current_year + '-' + current_month + '-' + current_date) //需要的数据 比如: 6-18