Skip to content

Instantly share code, notes, and snippets.

@dhotson
Created January 23, 2022 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhotson/50809652b90efbac71b617eaa1ec62fd to your computer and use it in GitHub Desktop.
Save dhotson/50809652b90efbac71b617eaa1ec62fd to your computer and use it in GitHub Desktop.
# require 'ap'
def months_between(d1, d2)
(d2.year * 12 + d2.month) - (d1.year * 12 + d1.month)
end
today = Date.today
first = (today - 10.weeks).beginning_of_month
last = today.end_of_month
cal = {}
(first..last).each do |date|
week_number = (date - today + today.wday).to_i / 7
day_number = (date - today + today.wday).to_i % 7
month = months_between(first, date)
row = week_number + month
col = day_number
cal[row] ||= []
cal[row][col] = date
end
# ap cal
(cal.keys.min..cal.keys.max).each do |row|
days_in_week = cal[row]
if days_in_week
(0...7).each do |col|
date = days_in_week[col]
print date ? " %2d" % date.day : " "
end
end
print "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment