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

**URL:** <https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724>\
**Category:** Rails\
**Created:** [June 1, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724 "2007-06-01T20:14:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 1, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724/1 "2007-06-01T20:14:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 1, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724/2 "2007-06-01T20:49:55Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 1, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724/3 "2007-06-01T20:52:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 1, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724/4 "2007-06-01T20:54:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 1, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724/5 "2007-06-01T21:17:59Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 1, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724/6 "2007-06-01T21:13:59Z")

</div>

On Jun 1, 1:54 pm, Joe P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:

> **[Ruby-Doc.org: Documenting the Ruby Language](https://ruby-doc.org/)**
>
> Fast, searchable Ruby documentation for core and standard libraries. Plus, links to tutorials, guides, books, and related sites.

The search function leads you to:  
[http://facets.rubyforge.org/src/doc/rdoc/more/classes/Time.html#M000174](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:

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 1, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724/7 "2007-06-01T21:23:47Z")

</div>

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](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/](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](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…

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 1, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724/8 "2007-06-01T22:40:56Z")

</div>

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](http://agileconsultingllc.com)  
> > > removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 1, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/easy-question-how-to-set-the-day-for-a-time-object/105724/9 "2007-06-01T22:09:07Z")

</div>

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](http://agileconsultingllc.com)  
> > removed\_email\_address@domain.invalid
