how to get the last week start date and end date based on date in current week ruby (Monday - Sunday)
Example: if date is 04-feb-15, then result should be start_date = Monday(26-jan-1015) end_date = Sunday(1-feb-15)
if date is 27-feb-15, , then result should be start_date = Monday(16-feb-1015) end_date = (22-feb-15)
You can try this way:
require 'date'
=> true
date = Date.today
=> #<Date: 2015-02-04 ((2457058j,0s,0n),+0s,2299161j)>
# date.wday return week day
end_date = date-date.wday
=> #<Date: 2015-02-01 ((2457055j,0s,0n),+0s,2299161j)>
start_date = date-date.wday-6
=> #<Date: 2015-01-26 ((2457049j,0s,0n),+0s,2299161j)>
For more operations you can refer this link Ruby Doc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With