# Time.to\_s

**URL:** <https://www.ruby-forum.com/t/time-to-s/108268>\
**Category:** Ruby\
**Created:** [July 1, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/time-to-s/108268 "2007-07-01T12:22:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonas](https://www.ruby-forum.com/u/jonas)\
**Post date:** [July 1, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/time-to-s/108268/1 "2007-07-01T12:22:26Z")

</div>

I’m overriding Time.to\_s to use a custom date format.  
The method get’s an argument, nothing in the documentation says that it  
should. How do I find out what the argument does?  
Where can I find the sourcecode for the original Time.to\_s?

---

<div class="post-metadata">

**Author:** ![jonas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonas](https://www.ruby-forum.com/u/jonas)\
**Post date:** [July 1, 2007, 12:38pm UTC](https://www.ruby-forum.com/t/time-to-s/108268/2 "2007-07-01T12:38:09Z")

</div>

On 7/1/07, Jonas [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m overriding Time.to\_s to use a custom date format.  
> The method get’s an argument, nothing in the documentation says that it  
> should.  
> Hmm that is strange, I cannot pass any argument to Time#to\_s, can you  
> specify your version?  
> Robert

---

<div class="post-metadata">

**Author:** ![jonas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonas](https://www.ruby-forum.com/u/jonas)\
**Post date:** [July 1, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/time-to-s/108268/3 "2007-07-01T17:35:41Z")

</div>

Try Time#strftime  
(string format time)  
It is similar to the formatting found in other languages. You can  
define your own method and simply use part of this internally in you  
method:

def my\_time\_format(Time)  
Time.strftim(%D%M)  
…other code…  
end

It is really that simple.

---

<div class="post-metadata">

**Author:** ![jonas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonas](https://www.ruby-forum.com/u/jonas)\
**Post date:** [July 1, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/time-to-s/108268/4 "2007-07-01T12:42:09Z")

</div>

Hi Jonas,

the Object.to\_s method should not expect an argument since it is per  
default an alias for Object.name.

If you want a custom time format you should overwrite the instance  
method. There are several ways to accomplish this. For example you  
derive from Time:

class CustomTime \< Time  
def to\_s  
strftime ‘Simon says: Do your %d sit-ups on every %A.’  
end  
end

HF  
Florian
