codenamev
12/3/2013 - 9:20 PM

Find the nearest Monday

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)