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.

            
              <div id="calendar"></div>
            
          
!
            
              $day_size: 70;
$main_color: #4f93c8;

@import url(https://fonts.googleapis.com/css?family=Lato:300);

body {
  background: #ccc;
  font-family: Lato, sans-serif;
  font-weight: 300;
}

#calendar {
  width: 7 * $day_size + px;
  margin: 0 auto;
  
  h2 {
    line-height: 170px;
    margin: 0;
    font-weight: 300;
    color: #fff;
    background: $main_color;
    text-align: center;
  }
  
  table {
    width: 100%;
    margin-top: -42px;
    border-collapse: collapse;
  }
  
  .week-days {
    color: #fff;
  }
  
  .day {
    width: $day_size + px;
    height: 40px;
    text-align: center;
  }
  
  .week {
    .day {
      background: #eee;
    }
  }
  
  .other-month {
    opacity: 0.5;
  }
}
            
          
!
            
              function Calendar(month, year) {
  var now = new Date();

  // labels for week days and months
  var days_labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      months_labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

  // test if input date is correct, instead use current month
  this.month = (isNaN(month) || month == null) ? now.getMonth() + 1 : month;
  this.year = (isNaN(year) || year == null) ? now.getFullYear() : year;

  var logical_month = this.month - 1;

  // get first day of month and first week day
  var first_day = new Date(this.year, logical_month, 1),
      first_day_weekday = first_day.getDay() == 0 ? 7 : first_day.getDay();

  // find number of days in month
  var month_length = new Date(this.year, this.month, 0).getDate(),
      previous_month_length = new Date(this.year, logical_month, 0).getDate();

  // calendar header
  var html = '<h2>' + months_labels[logical_month] + ' ' + this.year + '</h2>';

  // calendar content
  html += '<table class="calendar-table">';

  // week days labels row
  html += '<thead>';
  html += '<tr class="week-days">';
  for (var i = 0; i <= 6; i++) {
    html += '<th class="day">';
    html += days_labels[i];
    html += '</th>';
  }
  html += '</tr>';
  html += '</thead>';

  // define default day variables
  var day  = 1, // current month days
      prev = 1, // previous month days
      next = 1; // next month days

  html += '<tbody>';
  html += '<tr class="week">';
  // weeks loop (rows)
  for (var i = 0; i < 9; i++) {
    // weekdays loop (cells)
    for (var j = 1; j <= 7; j++) {
      if (day <= month_length && (i > 0 || j >= first_day_weekday)) {
        // current month
        html += '<td class="day">';
        html += day;
        html += '</td>';
        day++;
      } else {
        if (day <= month_length) {
          // previous month
          html += '<td class="day other-month">';
          html += previous_month_length - first_day_weekday + prev + 1;
          html += '</td>';
          prev++;
        } else {
          // next month
          html += '<td class="day other-month">';
          html += next;
          html += '</td>';
          next++;
        }
      }
    }

    // stop making rows if it's the end of month
    if (day > month_length) {
      html += '</tr>';
      break;
    } else {
      html += '</tr><tr class="week">';
    }
  }
  html += '</tbody>';
  html += '</table>';

  return html;
}

// document.getElementById('calendar').innerHTML = Calendar(12, 2015); 
document.getElementById('calendar').innerHTML = Calendar();
            
          
!
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