-
-
Save erkiesken/54bdbc069ba7df21eab6e4e4808f56a5 to your computer and use it in GitHub Desktop.
compact calendar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! You can move the offset calculation to (modern-ish) CSS to save few lines. (See my fork.)