ruby - Get the last Sunday of the Month -
i'm using chronic last sunday of month of given year. gladly give me nth sunday, not last.
this works, not need:
chronic.parse('4th sunday in march', :now => time.local(2015,1,1)) this need, doesn't work:
chronic.parse('last sunday in march', :now => time.local(2015,1,1)) is there way around apparent limitation?
update:  i'm upvoting 2 answers below because they're both good, i've implemented in "pure ruby" (in 2 lines of code, besides require 'date' line), i'm trying demonstrate management ruby right language use replace java codebase going away (and had dozens of lines of code compute this), , told 1 manager in 1 line of ruby, , readable , easy maintain.
i not sure chronic (i haven't heared before), can implement in pure ruby :)
## # returns date object being last sunday of given month/year # month: integer between 1 , 12 def last_sunday(month,year)   # last day of month   date = date.new year, month, -1   #subtract number of days ahead of sunday   date -= date.wday end the last_sunday method can used this:
last_sunday 07, 2013 #=> #<date: 2013-07-28 ((2456502j,0s,0n),+0s,2299161j)> 
Comments
Post a Comment