-
-
Save dawidof/b5e5a13247c5335f309f5dbcbcd533a6 to your computer and use it in GitHub Desktop.
Find the nearest Monday
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
# 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