Last active
June 27, 2021 10:49
-
-
Save fzn0x/69d0997753342ff03cb2a12824d9d53f to your computer and use it in GitHub Desktop.
JS Console Calendar
This file contains hidden or 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
let th = {"Su":[],"Mo":[],"Tu":[],"We":[],"Th":[],"Fr":[],"Sa":[]}; | |
const tk = Object.keys(th); | |
const now = new Date(); | |
const month = new Date(now.getFullYear(), now.getMonth()+1, 0); | |
const daysInMonth = month.getDate(); | |
let moI = month.getDay()-1; | |
// fill days before | |
for (let i = 0; i < moI; i++) { | |
th[tk[i%tk.length]].push(0); | |
} | |
let max = 0; | |
[...Array(daysInMonth)].map(x => { | |
let s = th[tk[moI%tk.length]]; | |
s.push(moI-1); | |
if (s.length > max) max = s.length; | |
moI++ | |
}); | |
let rend = [tk.join(' ')]; | |
let rends = [...Array(tk.length)].map(x => []); | |
tk.map(d => { | |
let v = th[d]; | |
const fill = max - v.length; | |
[...Array(fill)].map(x => v.push(0)) | |
v.forEach((value,i) => rends[i].push(value)) | |
}) | |
for (let i = 0; i < rends.length; i++) { | |
rend.push(rends[i] | |
.map(d => d == 0 ? " " : d) | |
.join(' ')) | |
} | |
console.log(rend) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment