Last active
February 23, 2021 15:56
-
-
Save ztrehagem/1630107b23ec2628f83bf0cfcf2acd17 to your computer and use it in GitHub Desktop.
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
const ALIGN_WEEK_NUM = false | |
const now = new Date() | |
const [ | |
year = now.getFullYear().toString(), | |
month = (now.getMonth() + 1).toString(), | |
] = process.argv.slice(2) | |
const targetYear = parseInt(year) | |
const targetMonth = parseInt(month) - 1 | |
const startOfMonth = new Date(targetYear, targetMonth, 1) | |
const endOfMonth = new Date(targetYear, targetMonth + 1, 0) | |
const datesOfMonth = endOfMonth.getDate() | |
const padStart = startOfMonth.getDay() | |
const padEnd = 6 - endOfMonth.getDay() + (ALIGN_WEEK_NUM && padStart + datesOfMonth <= 7 * 5 ? 7 : 0) | |
const dates = [ | |
...[...new Array(padStart)].map((_, i) => new Date(targetYear, targetMonth, - padStart + i + 1)), | |
...[...new Array(datesOfMonth)].map((_, i) => new Date(targetYear, targetMonth, i + 1)), | |
...[...new Array(padEnd)].map((_, i) => new Date(targetYear, targetMonth, datesOfMonth + i + 1)), | |
] | |
const weeks = [...new Array(dates.length / 7)].map((_, i) => dates.slice(i * 7, i * 7 + 7)) | |
console.log(`${targetYear} / ${targetMonth + 1}`) | |
for (const week of weeks) { | |
console.log(week.map((d) => (' ' + d.getDate().toString()).slice(-2)).join(' ')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment