Skip to content

Instantly share code, notes, and snippets.

@ztrehagem
Last active February 23, 2021 15:56
Show Gist options
  • Save ztrehagem/1630107b23ec2628f83bf0cfcf2acd17 to your computer and use it in GitHub Desktop.
Save ztrehagem/1630107b23ec2628f83bf0cfcf2acd17 to your computer and use it in GitHub Desktop.
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