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?

On Jun 1, 2007, at 2:14 PM, Joe P. wrote:

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 B. http://agileconsultingllc.com
[email protected]

Joe P. wrote:

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?

@start_date = 1.month.from_now.beginning_of_month

Should work for ya.

Time.now.next_month.beginning_of_month
=> Sun Jul 01 00:00:00 -0400 2007
That is perfect. Thank you; is there a list of these methods somewhere?
I’ve been looking for them, but haven’t found them…

On Jun 1, 2007, at 2:54 PM, Joe P. wrote:

Time.now.next_month.beginning_of_month
=> Sun Jul 01 00:00:00 -0400 2007
That is perfect. Thank you; is there a list of these methods
somewhere?
I’ve been looking for them, but haven’t found them…


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 B. http://agileconsultingllc.com
[email protected]

On Jun 1, 1:54 pm, Joe P. [email protected] wrote:

Time.now.next_month.beginning_of_month
=> Sun Jul 01 00:00:00 -0400 2007

That is perfect. Thank you; is there a list of these methods somewhere?
I’ve been looking for them, but haven’t found them…


Posted viahttp://www.ruby-forum.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:

If you’re just looking for all the methods for the Time class
specifically, I’d suggest trying here:

http://www.ruby-doc.org/core/classes/Time.html

There’s also a list of some of the classes and methods here:

http://dev.rubycentral.com/ref/

It doesn’t look to be 100% complete, but there are a lot of methods
listed for many of the built-in classes, including Time

http://dev.rubycentral.com/ref/ref_c_time.html

The Ruby Cookbook can be helpful also, giving suggestions on different
problems and how to solve them. I need to listen to the advice given
above, since I’m also new to Ruby and need to have patience.

That is perfect. Thank you; is there a list of these methods somewhere?
I’ve been looking for them, but haven’t found them…

Yes, good point, of course the Ruby classes wouldn’t include all the
Rails options. I just happened to have those handy because I was working
on some quick date formatting this morning. The Rails API is definitely
a more complete and appropriate choice for this list.

Rob B. wrote:

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

On Jun 1, 2007, at 3:23 PM, Sharon M. wrote:

It doesn’t look to be 100% complete, but there are a lot of methods

I’ve been looking for them, but haven’t found them…
Rob B. http://agileconsultingllc.com
[email protected]

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

On Jun 1, 2007, at 3:23 PM, Sharon M. wrote:

It doesn’t look to be 100% complete, but there are a lot of methods

I’ve been looking for them, but haven’t found them…
Rob B. http://agileconsultingllc.com
[email protected]