//获取本周每天的年月日
getDate() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var week = date.getDay();
this.setState({
activeWeek: week - 1
})
var month = month + 1;
var day = date.getDate();
// week:周几,day:几号
var weekArr = [{ week: '周一', day: '', month, year }, { week: '周二', day: '', month, year }, { week: '周三', day: '', month, year }, { week: '周四', day: '', month, year }, { week: '周五', day: '', month, year }, { week: '周六', day: '', month, year }, { week: '周天', day: '', month, year }];
// 本周内今天的前几天的数量
var leftNum = week - 1;
// 本周内今天的后几天的数量
var rightNum = 7 - week;
// 本周内今天的前几天
for (var i = 0; i < leftNum; i++) {
//判断本周含有上月日期
if (day - (week - i - 1) <= 0) {
if (month == 1) {
weekArr[i].day = new Date(year - 1, 12, 0).getDate() - week + i + 2
weekArr[i].year = year - 1
weekArr[i].month = 12
} else {
weekArr[i].day = new Date(year, month - 1, 0).getDate() - week + i + 2
if (month - 1 < 10) {
weekArr[i].month = '0' + (month - 1)
} else {
weekArr[i].month = month - 1
}
}
} else {
if (day - (week - i) + 1 < 10) {
weekArr[i].day = '0' + (day - (week - i) + 1);
} else {
weekArr[i].day = day - (week - i) + 1;
}
if (month < 10) {
weekArr[i].month = '0' + month
} else {
weekArr[i].month = month
}
}
}
// 本周内今天的后几天
for (var i = 0; i < rightNum; i++) {
if (day + i + 1 > 31) {
if (month == 12) {
weekArr[i + week].day = '0' + (i + 1)
weekArr[i].year = year + 1
weekArr[i].month = '0' + 1
} else {
weekArr[i + week].day = '0' + (i + 1)
if (month + 1 < 10) {
weekArr[i + week].month = '0' + (month + 1)
} else {
weekArr[i + week].month = month + 1
}
}
} else {
if (day + i + 1 < 10) {
weekArr[i + week].day = '0' + (day + i + 1);
} else {
weekArr[i + week].day = day + i + 1;
}
if (month < 10) {
weekArr[i + week].month = '0' + month
} else {
weekArr[i + week].month = month
}
}
}
// 今天
if (day < 10) {
weekArr[week - 1].day = '0' + day;
} else {
weekArr[week - 1].day = day;
}
if (month < 10) {
weekArr[week - 1].month = '0' + month
} else {
weekArr[week - 1].month = month
}
}