Skip to content

Instantly share code, notes, and snippets.

@zazaulola
Last active September 11, 2024 10:09
Show Gist options
  • Save zazaulola/8516bb368b9aebae509f6b45a138d345 to your computer and use it in GitHub Desktop.
Save zazaulola/8516bb368b9aebae509f6b45a138d345 to your computer and use it in GitHub Desktop.
Nano-calendar on javascript without elements rendering (data only)
// Calendar for month `m` of the year `y` with Sunday as first day of week
const calS = (y,m) => {
let[r,d,w,l]=[[],1,0,!(y%4)-!(y%1e2)+!(y%4e2)],
c=((0|23*m/9)+(m<3?y--:y-2)+5+(0|y/4)-(0|y/1e2)+(0|y/4e2))%7;
for(;d<29+(0x3bbeecc+16*l>>m*2&3);++d,c=++c%7,w+=!c)
(r[w]||(r[w]=[,,,,,,,]))[c]=d;
return r
}
// Calendar for month `m` of the year `y` with Monday as first day of week
const calM = (y,m) => {
let[r,d,w,l]=[[],1,0,!(y%4)-!(y%1e2)+!(y%4e2)],
c=((0|23*m/9)+(m<3?y--:y-2)+5+(0|y/4)-(0|y/1e2)+(0|y/4e2))%7||7;
for(;d<29+(0x3bbeecc+16*l>>m*2&3);++d,c=++c%7||7,w+=!(c-1))
(r[w]||(r[w]=[,,,,,,,]))[c-1]=d;
return r
}
// Calendar for month `m` of the year `y` with `s` as first day of week
// s=0 - Sunday s=1 - Monday
const cal = (y,m,s=0)=>{
let [r,d,w,l]=[[],1,0,!(y%4)-!(y%1e2)+!(y%4e2)],
c=((0|23*m/9)+(m<3?y--:y-2)+5+(0|y/4)-(0|y/1e2)+(0|y/4e2))%7||(s?7:0);
for (;d<29+(0x3bbeecc+16*l>>m*2&3);++d,c=++c%7||(s?7:0),w+=!(c-s))
(r[w]||(r[w]=[,,,,,,,]))[c-s]=d;
return r;
};
/********* Test *********/
console.log(calS(2020,8));
/*
[
[ , , , , , , 1 ],
[ 2, 3, 4, 5, 6, 7, 8 ],
[ 9, 10, 11, 12, 13, 14, 15 ],
[ 16, 17, 18, 19, 20, 21, 22 ],
[ 23, 24, 25, 26, 27, 28, 29 ],
[ 30, 31, , , , , ]
]
*/
console.log(calM(2020,8));
/*
[
[ , , , , , 1, 2 ],
[ 3, 4, 5, 6, 7, 8, 9 ],
[ 10, 11, 12, 13, 14, 15, 16 ],
[ 17, 18, 19, 20, 21, 22, 23 ],
[ 24, 25, 26, 27, 28, 29, 30 ],
[ 31, , , , , , ]
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment