Or yes, using Date rather than Time as Jamie showed.
Current versions of ActiveSupport handle this properly, but older
versions that treat 1.month simply as 30 days of seconds would fail to
do the expected thing for dates in March (or any dates in months that
follow a 31 day month – yeah, it’s not looking too good).
However, you can get what you want with nothing more than the standard
Ruby Date class:
Current versions of ActiveSupport handle this properly, but older versions
that treat 1.month simply as 30 days of seconds would fail to do the
expected thing for dates in March (or any dates in months that follow a 31
day month – yeah, it’s not looking too good).
ah… the old parsing “31st Feb” giving no error, but instead
returning “3rd March”
You can also avoid errors given by “Date.today.beginning_of_month -
1.month” or “Date.today.beginning_of_month.ago(1.month)” by using
“Date.today.beginning_of_month.ago(1.day).beginning_of_month” as
another alternative option.
OT The problem is very old. TSO, Time Sharing Option, was IBM’s first
commercial timesharing product. Its interactivity offered the first way
for
most enterprise customers off punched cards. When the date first went
from
February 28 to February 29 in 1972, the date on the session startup
message
read “March 0”.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.