css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <table class="calendar">
  <caption>
    <!-- スクリプトで中身を生成 -->
  </caption>
  <thead>
    <tr>
      <th>Sun</th>
      <th>Mon</th>
      <th>Tue</th>
      <th>Wed</th>
      <th>Thu</th>
      <th>Fri</th>
      <th>Sat</th>      
    </tr>
  </thead>
  <tbody>
    <!-- スクリプトで中身を生成 -->
  </tbody>
</table>
            
          
!
            
              /* カレンダー全体の設定 */
.calendar {
  border-collapse: collapse;
}

.calendar th,
.calendar td {
  width: 40px;
  height: 25px;
  border: 1px solid #000;
  text-align: center;
}

/* 日曜日 */
.calendar th:nth-child(1),
.calendar td:nth-child(1) {
  color: #f00;
  background-color: #fcc;
}

/* 土曜日 */
.calendar th:nth-child(7),
.calendar td:nth-child(7) {
  color: #00f;
  background-color: #ccf;
}
            
          
!
            
              // カレンダーのHTMLを参照
const calendar = document.querySelector('.calendar');
const calendarBody = calendar.querySelector('tbody');
const calendarCaption = calendar.querySelector('caption');

// 基準となる日付の年と月を取得
const now = new Date();
const nowYear = now.getFullYear();
const nowMonth = now.getMonth();

// カレンダーのタイトルを表示
// ※ getMonthの結果は0~11なので、表示するときには1を加えます
calendarCaption.innerHTML = nowYear + '年' + (nowMonth + 1) + '月のカレンダー'; 

// 月の初日の曜日と、月の末日の日付を取得
// ※ 末日は、翌月1日の1日前(1 - 1 = 0)で求めることができます
const firstDayOfMonth = new Date(nowYear, nowMonth, 1).getDay();
const lastDateOfMonth = new Date(nowYear, nowMonth + 1, 0).getDate();

// カレンダーは最大で6週間(6行)となるため、
// とりあえず6行になるよう、1~42の配列を用意する
const dateArray = [
   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, 30, 31, 32, 33, 34, 35,
  36, 37, 38, 39, 40, 41, 42
].map(function (date) {
  return date - firstDayOfMonth
});

// 6週間(6行)あるため、6回ループする
for (let i = 0; i < 6; i++) {
  // カレンダーに行を挿入する
  const weekRow = calendarBody.insertRow(-1);
  // 日付の配列から1行分(1週間分)の数字を切り取る
  const dateSubArray = dateArray.slice(i * 7, i * 7 + 7)
  // 切り取った配列をtdタグに変換し、結合して行に挿入する
  weekRow.innerHTML = dateSubArray.map(function (date) {
    // 配列の数字が1より小さいか、月の末日より大きい場合は空白セル
    date = date < 1 || date > lastDateOfMonth ? '' : date;
    return '<td>' + date + '</td>'
  }).join('');
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console