uniapp获取本周的所有日期(直接cv)

const getTimeandWeek = () => {
  var weekDay = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
  var now = new Date();
  // 获取本周的第一天(星期一)
  var firstDay = new Date(now.setDate(now.getDate() - now.getDay() + (now.getDay() === 0 ? -6 : 1)));
  // 存储本周的日期
  var weekDates = [];
  // 循环获取本周的所有日期
  for (var i = 0; i < 7; i++) {
    var tempDate = new Date(firstDay);
    tempDate.setDate(firstDay.getDate() + i);
    var year = tempDate.getFullYear();
    var month = tempDate.getMonth() + 1;
    var day = tempDate.getDate();
    weekDates.push({
      week: weekDay[i],
      date: year + '-' + (month < 10 ? '0' : '') + month + '-' + (day < 10 ? '0' : '') + day
    });
  }
  console.log(weekDates)
}
getTimeandWeek()

你可能感兴趣的:(uniapp,uni-app,javascript,前端)