Skip to content

Instantly share code, notes, and snippets.

@erkiesken
Created June 29, 2022 13:30
Show Gist options
  • Save erkiesken/54bdbc069ba7df21eab6e4e4808f56a5 to your computer and use it in GitHub Desktop.
Save erkiesken/54bdbc069ba7df21eab6e4e4808f56a5 to your computer and use it in GitHub Desktop.
compact calendar
<html>
<body>
<style>
.day {
display: inline-block;
width: 20px;
height: 20px;
background: #EEE;
text-align: center;
line-height: 20px;
}
.day.weekend {
background: #CCC;
}
.day:first-child[data-weekday="1"] {
margin-left: 0px;
}
.day:first-child[data-weekday="2"] {
margin-left: 20px;
}
.day:first-child[data-weekday="3"] {
margin-left: 40px;
}
.day:first-child[data-weekday="4"] {
margin-left: 60px;
}
.day:first-child[data-weekday="5"] {
margin-left: 80px;
}
.day:first-child[data-weekday="6"] {
margin-left: 100px;
}
.day:first-child[data-weekday="0"] {
margin-left: 120px;
}
</style>
<script>
const cal = document.createElement('div');
cal.classList.add('calendar');
const y = new Date().getFullYear();
for (let m = 1; m <= 12; m++) {
const days = new Date(y, m, 0).getDate();
const row = document.createElement('div');
row.classList.add('month');
row.dataset.month = m;
for (let day = 1; day <= days; day++) {
const cell = document.createElement('div');
const d = new Date(y, m - 1, day).getDay();
cell.innerText = day;
cell.classList.add('day');
if (d == 0 || d == 6) {
cell.classList.add('weekend');
}
cell.dataset.weekday = d;
row.appendChild(cell);
}
cal.appendChild(row);
}
document.body.appendChild(cal);
</script>
</body>
</html>
@myfonj
Copy link

myfonj commented Jun 29, 2022

Nice! You can move the offset calculation to (modern-ish) CSS to save few lines. (See my fork.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment