Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="calendar">
  <div class="info">июль 2018</div>
  
  <table>
    <thead>
    <tr>
      <th>пн</th>
      <th>вт</th>
      <th>ср</th>
      <th>чт</th>
      <th>пт</th>
      <th>сб</th>
      <th>вc</th>
    </tr>
    
    </thead>
    <tbody class="dates">
    
    
    </tbody>
  </table>
  <div class="nav">
    <a href="" class="prev">&larr;</a>
    <a href="" class="next">&rarr;</a>
  </div>

</div>

              
            
!

CSS

              
                body {
  background-color: #444444;
}

#calendar {
  display: inline-block;
  margin: 20px auto;

  background-color: #f9f9f9;
  margin-left: 40%;
  margin-top: 100px;
  border: 5px solid #d69d36;
}

#calendar td, #calendar th {
  padding: 10px;
  border: 1px solid black;
  text-align: center;
}

#calendar .nav, #calendar .info {
  text-align: center;
}

#calendar a {
  color: blue;
  font-size: 25px;
  text-decoration: none;
}

#calendar a:hover {
  color: red;
}

.currentDate {
  font-weight: 900;
  color: #f9f9f9;
  background-color: #d69d36;
}

              
            
!

JS

              
                const doc = document;
const calendar = doc.getElementById('calendar');
const prev = calendar.getElementsByClassName('prev')[0];
const next = calendar.getElementsByClassName('next')[0];

const date = new Date();
let showedYear = date.getFullYear();
let showedMonth = date.getMonth();


const nextPrevData = {
  getPrevYear: (year, month) => (month === 0) ? year - 1 : year,
  getPrevMonth: (month) => (month === 0) ? 11 : month - 1,
  getNextYear: (year, month) => (month === 11) ? year + 1 : year,
  getNextMonth: (month) => (month === 11) ? 0 : month + 1
};

prev.addEventListener('click', function(event) {
  event.preventDefault();
  
  showedYear = nextPrevData.getPrevYear(showedYear, showedMonth); //получаем предыдущий год
  showedMonth = nextPrevData.getPrevMonth(showedMonth); // получаем предыдущий месяц
  
  initCalendar(showedYear, showedMonth, calendar);
});

next.addEventListener('click', function(event) {
  event.preventDefault();
  
  showedYear = nextPrevData.getNextYear(showedYear, showedMonth); //получаем предыдущий год
  showedMonth = nextPrevData.getNextMonth(showedMonth); // получаем предыдущий месяц
  
  initCalendar(showedYear, showedMonth, calendar);
});

/**************************************************/

const monthOrWeak = {
  getRealDayOfWeekNum: (jsNumOfDay) => (jsNumOfDay === 0) ? 7 : jsNumOfDay, // если ноль,то возврашщаем воскресенье
  getFirstWeekDayOfMonthNum: (year, month) => new Date(year, month, 1).getDay(), //получает первый день недели  месяца
  getLastWeekDayOfMonthNum: (year, month) => new Date(year, month + 1, 0).getDay(),
  getLastDayOfMonth: (year, month) => new Date(year, month + 1, 0).getDate(), // getDate() -это день недели
  getUnshiftElemsNum: (year, month) => {
    const jsDayNum = monthOrWeak.getFirstWeekDayOfMonthNum(year, month);
    return monthOrWeak.getRealDayOfWeekNum(jsDayNum) - 1;
  },
  getPushElemsNum: (year, month) => {
    const jsDayNum = monthOrWeak.getLastWeekDayOfMonthNum(year, month);
    return 7 - monthOrWeak.getRealDayOfWeekNum(jsDayNum);
  }
};

/**************************************************/

const changeArr = {
  createArr: (from, to) => {
    const arr = [];
    
    for (let i = from; i <= to; i++) {
      arr.push(i);
    }
    
    return arr;
  },
  unshiftElems: (num, elem, arr) => {
    for (let i = 0; i < num; i++) {
      arr.unshift(elem);
    }
    
    return arr;
  },
  pushElems: (num, elem, arr) => {
    for (let i = 0; i < num; i++) {
      arr.push(elem);
    }
    
    return arr;
  },
  chunkArr: (num, arr) => {
    const result = [];
    let chunk = [];
    const iterCount = arr.length / num;
    
    for (let i = 0; i < iterCount; i++) {
      chunk = arr.splice(0, num);
      result.push(chunk);
    }
    
    return result;
  }
};

/**************************************************/

function drawDates(year, month, dates) { //отрисовываем даты внутри функции initCalendar()
  let arr = [];
  const firstDateOfMonth = 1;
  const LastDateOfMonth = monthOrWeak.getLastDayOfMonth(year, month);
  
  const unshiftElemsNum =  monthOrWeak.getUnshiftElemsNum(year, month);
  const pushElemsNum = monthOrWeak.getPushElemsNum(year, month);
  
  arr = changeArr.createArr(firstDateOfMonth, LastDateOfMonth);
  arr = changeArr.unshiftElems(unshiftElemsNum, '', arr);
  arr = changeArr.pushElems(pushElemsNum, '', arr);
  arr = changeArr.chunkArr(7, arr);
  
  const currentDate = { //получаем текущую дату
    year: new Date().getFullYear(),
    month: new Date().getMonth(),
    day: new Date().getDate()
  };
  const checkDate = (day) => (year === currentDate.year && month === currentDate.month && day === currentDate.day); //проверка соответстует ли выводимая дата при отрисове,- текущей
  
  const createTable = (arr, parent) => {
    parent.innerHTML = '';
    
    for(let i = 0; i < arr.length; i++){
      const tr = doc.createElement('tr');
      
      for(let j = 0; j < arr[i].length; j++){
        const td = doc.createElement('td');
        const day = arr[i][j];
  
        if (checkDate(day)) { //если дата при отрисовке совпала с текущей
          td.className = 'currentDate'; //присвоить этой ячейке класс
        }
        td.innerHTML = day;
        tr.appendChild(td);
      }
      
      parent.appendChild(tr);
    }
  };
  
  createTable(arr, dates);
}

function initCalendar(year, month, calendar){ //эта функция будет отвечать за начальную  инициализацию календаря,и когда будем жать на стрелки
  const dates = calendar.getElementsByClassName('dates')[0]; //по нажатию на стрелку будет получится дом элемент
  const info = calendar.getElementsByClassName('info')[0];
  
  const showInfo = (year, month, elem) => { //отрисовываем год над календаре
    const monthes = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'];
    
    elem.innerHTML = monthes[month] + ' ' + year;
  };
  
  drawDates(year, month, dates);
  showInfo(year, month, info); //отрисовываем год над календарем
}

initCalendar(showedYear, showedMonth, calendar); //функция которую вызываем напрямую в нее будем передовать год,месяц и дом элемент

              
            
!
999px

Console