Hello everyone.
Please advice how to get todays month first day and previous month first
day.
If go on by this post date, it will need to give me now
01-11-2010
and
01-10-2010
or if today was like 3 Jan 2010 it would be
01-01-2010
and
01-12-2009
Something like:
Date.today.beginning_of_month
Date.today.ago(1.month).beginning_of_month
Probably an easier way, just off the top of my head.
Jamey
Vitaliy Y. wrote in post #958983:
Hello everyone.
Please advice how to get todays month first day and previous month first
day.
If go on by this post date, it will need to give me now
01-11-2010
and
01-10-2010
or if today was like 3 Jan 2010 it would be
01-01-2010
and
01-12-2009
ruby-1.9.2-p0 > today = Time.now
=> 2010-11-03 10:02:46 -0400
ruby-1.9.2-p0 > today.at_beginning_of_month
=> 2010-11-01 00:00:00 -0400
ruby-1.9.2-p0 > today.beginning_of_month
=> 2010-11-01 00:00:00 -0400
ruby-1.9.2-p0 > today.beginning_of_month - 1.month
=> 2010-10-01 00:00:00 -0400
Robert W. wrote in post #958994:
ruby-1.9.2-p0 > today = Time.now
=> 2010-11-03 10:02:46 -0400
ruby-1.9.2-p0 > today.at_beginning_of_month
=> 2010-11-01 00:00:00 -0400
ruby-1.9.2-p0 > today.beginning_of_month
=> 2010-11-01 00:00:00 -0400
ruby-1.9.2-p0 > today.beginning_of_month - 1.month
=> 2010-10-01 00:00:00 -0400
Jamey C. wrote
Date.today.beginning_of_month
Or yes, using Date rather than Time as Jamie showed.
On Nov 3, 2010, at 10:09 AM, Robert W. wrote:
Jamey C. wrote
Date.today.beginning_of_month
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:
[ruby-1.9.2-p0] :Users/rab $ irb
irb> require ‘date’
=> true
irb> t = Date.today
=> #<Date: 2010-11-03 (4911007/2,0,2299161)>
irb> puts t
2010-11-03
=> nil
irb> bom = t - t.mday + 1
=> #<Date: 2010-11-01 (4911003/2,0,2299161)>
irb> puts bom
2010-11-01
=> nil
irb> prev = bom << 1
=> #<Date: 2010-10-01 (4910941/2,0,2299161)>
irb> puts prev
2010-10-01
=> nil
-Rob
Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/
On 3 November 2010 14:56, Rob B. [email protected]
wrote:
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.
Dates and times are horrible…
Thanks all, the method beginning_of_month is just what I was looking
for.
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”.