Forum: Ruby on Rails Easy Question: How to set the day for a Time object?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2119f016588ba13373484491bd2dd6d1?d=identicon&s=25 Joe Peck (fatcatt316)
on 2007-06-01 20:14
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?
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-01 20:49
(Received via mailing list)
On Jun 1, 2007, at 2:14 PM, Joe Peck 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
2da252cc1d2fb3701126171831b50a65?d=identicon&s=25 Dave Coleman (pezdude)
on 2007-06-01 20:52
Joe Peck 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.
2119f016588ba13373484491bd2dd6d1?d=identicon&s=25 Joe Peck (fatcatt316)
on 2007-06-01 20:54
>  >> 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...
8716cce9a7b721786e5425b8cc1f9951?d=identicon&s=25 unknown (Guest)
on 2007-06-01 21:13
(Received via mailing list)
On Jun 1, 1:54 pm, Joe Peck <rails-mailing-l...@andreas-s.net> 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:
http://www.ruby-doc.org/

The search function leads you to:
http://facets.rubyforge.org/src/doc/rdoc/more/clas...

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:
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-01 21:17
(Received via mailing list)
On Jun 1, 2007, at 2:54 PM, Joe Peck 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
C5997031f8a5be985f2ae341276b618b?d=identicon&s=25 Sharon Machlis (sharon)
on 2007-06-01 21:23
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...
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-01 22:09
(Received via mailing list)
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 Machlis 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
C5997031f8a5be985f2ae341276b618b?d=identicon&s=25 Sharon Machlis (sharon)
on 2007-06-01 22:40
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 Biedenharn 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 Machlis 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 Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.