手写日历-数组组成

function packageArr () {
       let allDays = getMonthDay();
       // 查询有几周
       let weeks = allDays.length / 7 > 4 ? 5 : 4;
       // 查询每周一的下标
       let indexArr = [];
       allDays.map((val, index) => {
          if(val.week === 1){
              indexArr.push(index);
          }
       });
       // 如果第一天正好是 周一 则剔除indexArr[0]
       if(indexArr[0] === 0){
          indexArr.splice(0, 1);
       };
       // 将 每周塞进数组
       let arr = [];
       for(let i = 0;i < weeks;i++){
           if(i === 0){
              arr[i] = allDays.slice(0, indexArr[0]);
           }else{
              arr[i] = allDays.slice(indexArr[i-1], indexArr[i]);
           }
           if(arr[i].length < 7 && arr[i][0].week === 1){
               let diff = 7 - arr[i].length;
               for(let j = 0;j < diff;j++){
                  arr[i].push({});
               }
           }else if(arr[i].length < 7 && arr[i][0].week !== 1){
              let diff = 7 - arr[i].length;
              for(let j = 0;j < diff;j++){
                  arr[i].unshift({});
              }
           }
       };
       return arr;
    //   返回值
    //[
    //  [{}, {}, {}, {}, {day: '2021-01-01', week: 5},
{day: '2021-01-02', week: 6}, {day: '2021-01-03', week: 7},],
    //  [
    //    {day: '2021-01-04', week: 1}
    //    {day: '2021-01-05', week: 2}
    //    {day: '2021-01-06', week: 3}
    //    {day: '2021-01-07', week: 4}
    //    {day: '2021-01-08', week: 5}
    //    {day: '2021-01-09', week: 6}
    //    {day: '2021-01-10', week: 7}
    //  ],
    //  [
    //    {day: '2021-01-11', week: 1}
    //    {day: '2021-01-12', week: 2}
    //    {day: '2021-01-13', week: 3}
    //    {day: '2021-01-14', week: 4}
    //    {day: '2021-01-15', week: 5}
    //    {day: '2021-01-16', week: 6}
    //    {day: '2021-01-17', week: 7}
    //  ],
    //  [
    //    {day: '2021-01-18', week: 1}
    //    {day: '2021-01-19', week: 2}
    //    {day: '2021-01-20', week: 3}
    //    {day: '2021-01-21', week: 4}
    //    {day: '2021-01-22', week: 5}
    //    {day: '2021-01-23', week: 6}
    //    {day: '2021-01-24', week: 7}
    //  ],
    //  [
    //    {day: '2021-01-25', week: 1}
    //    {day: '2021-01-26', week: 2}
    //    {day: '2021-01-27', week: 3}
    //    {day: '2021-01-28', week: 4}
    //    {day: '2021-01-29', week: 5}
    //    {day: '2021-01-30', week: 6}
    //    {day: '2021-01-31', week: 7}
    //  ],
    //]
};
// 获取 月份 对应的所有日期
function getMonthDay () {
  let now = new Date();
  let current_month_num = mGetDate();
  let current_month = [];
  for (let i = 1; i <= current_month_num; i++) {
    let day = now.setDate(i);
    let everyDay = dateFormat(new Date(day), 'yyyy-MM-dd');
    current_month.push({
      day: everyDay,
      week: new Date(everyDay).getDay() === 0 ? 7 :new Date(everyDay).getDay()
    });
  };
  return current_month;
};
 
// 获取当前月的天数
function mGetDate () {
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var d = new Date(year, month, 0);
  return d.getDate();
};
 

 

posted @ 2021-03-10 15:26  Mr_R  阅读(85)  评论(0编辑  收藏  举报