Easy Question: How to set the day for a Time object?

Let's say I have this: @start_date = 1.month.from_now

I want to make sure that the "day" for @start_date is always 1. How can I change the day for @start_date?

--

Depending on what you want:

>> Time.now => Fri Jun 01 14:47:42 -0400 2007 >> Time.now.next_month.beginning_of_month => Sun Jul 01 00:00:00 -0400 2007 >> 1.month.from_now.beginning_of_week => Mon Jun 25 00:00:00 -0400 2007

Your choice.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

The short answer (though overwhelming) is here:

The search function leads you to: http://facets.rubyforge.org/src/doc/rdoc/more/classes/Time.html#M000174

Of course thats easy when you know the method name. If you are new to Ruby, have patience. You will learn. The pickaxe book is invaluable:

Look at the API docs for ActiveSupport::CoreExtensions::Time::Calculations

FYI, note the difference between:

2.months.from_now

and

Time.now.months_since(2) # aka, Time.now.next_month.next_month

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

But since the OP used .from_now which is a method added by ActiveSupport and this *is* the Rails list, it's appropriate to point at the Rails API rather than just the Ruby Time class docs. (Not that they wouldn't be helpful, you just won't find all of "these methods" if you limit "somewhere" to Ruby's core docs.)

-Rob