On Nov 3, 2010, at 10:09 AM, Robert Walker wrote:
Robert Walker 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 Cribbs 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 Biedenharn
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/
--
You received this message because you are subscribed to the Google Groups "Ruby on
Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.