Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how get the last week start date and end date based on date in current week ruby

Tags:

ruby

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)

like image 868
Mani David Avatar asked Dec 04 '25 13:12

Mani David


1 Answers

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.

like image 195
Sachin Gevariya Avatar answered Dec 07 '25 11:12

Sachin Gevariya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!