Todays month first day and previous month first day

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 Yanchuk 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 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.

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

require 'date'

=> true

t = Date.today

=> #<Date: 2010-11-03 (4911007/2,0,2299161)>

puts t

2010-11-03
=> nil

bom = t - t.mday + 1

=> #<Date: 2010-11-01 (4911003/2,0,2299161)>

puts bom

2010-11-01
=> nil

prev = bom << 1

=> #<Date: 2010-10-01 (4910941/2,0,2299161)>

puts prev

2010-10-01
=> nil

-Rob

Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/

Thanks all, the method beginning_of_month is just what I was looking
for.

ah... the old parsing "31st Feb" giving no error, but instead
returning "3rd March" :slight_smile:

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...

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”.