Skip to content

Instantly share code, notes, and snippets.

@codenamev
Created December 3, 2013 21:20
Show Gist options
  • Save codenamev/7777679 to your computer and use it in GitHub Desktop.
Save codenamev/7777679 to your computer and use it in GitHub Desktop.
Find the nearest Monday
# credit: http://stackoverflow.com/questions/4514988/rails-is-there-away-to-get-the-date-object-that-is-the-closest-monday-to-today
# docs: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-c-commercial
# Find the next Monday
Date.commercial(Date.today.year, 1+Date.today.cweek, 1)
# Find the nearest Monday
Date.commercial(Date.today.year, Date.today.cwday.modulo(4)+Date.today.cweek, 1)
# Date of writing was: 2013-12-03
Date.commercial Date.today.year, Date.today.cweek, 1 #=> Mon, 02 Dec 2013 (this weeks Monday)
Date.commercial Date.today.year, 1+Date.today.cweek, 1 #=> Mon, 09 Dec 2013 (next weeks Monday)
Date.commercial Date.today.year, 1+Date.today.cweek, 2 #=> Tue, 03 Dec 2013 (this weeks Tuesday)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment