Skip to content

Instantly share code, notes, and snippets.

@ii64
Forked from fzn0x/calendar.js
Created June 27, 2021 10:49
Show Gist options
  • Save ii64/f4ab44eba21ad90e867f1bf069ec6973 to your computer and use it in GitHub Desktop.
Save ii64/f4ab44eba21ad90e867f1bf069ec6973 to your computer and use it in GitHub Desktop.
JS Console Calendar
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