# Date.to\_s trimming

**URL:** <https://www.ruby-forum.com/t/date-to-s-trimming/64298>\
**Category:** Rails\
**Created:** [January 23, 2006, 2:09am UTC](https://www.ruby-forum.com/t/date-to-s-trimming/64298 "2006-01-23T02:09:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [January 23, 2006, 2:09am UTC](https://www.ruby-forum.com/t/date-to-s-trimming/64298/1 "2006-01-23T02:09:03Z")

</div>

When I convert a datetime to a string, it prints out something like this

Jan 20 12:00:00 EST 2006

I want to trim off the last twelve characters, i.e.

:00 EST 2006

I tried to do it like so:

@date =  
@meeting.date.to\_s.chomp.chomp.chomp.chomp.chomp.chomp.chomp.chomp.chomp.chomp.chomp.chomp

but the chomps seem to do nothing. I’d love to use regex, but I don’t  
know which method to use to remove text from a string, or to just sever  
the end of it (I tried unshift).

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [January 23, 2006, 2:23am UTC](https://www.ruby-forum.com/t/date-to-s-trimming/64298/2 "2006-01-23T02:23:55Z")

</div>

On Jan 22, 2006, at 5:09 PM, Mike S. wrote:

> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Mike-

```
Check out ri strftime in your shell it will show you a ton of

```

options for formatting times and dates. Here’s an example to do what  
you want:

in irb:

> > Time.now.strftime(“%b %d %I:%M”)  
> > =\> “Jan 22 05:23”

Cheers-

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [January 23, 2006, 2:48am UTC](https://www.ruby-forum.com/t/date-to-s-trimming/64298/3 "2006-01-23T02:48:19Z")

</div>

The response below would be the right way to do it, but to clear some  
things  
up for you:

To get a substring:  
“Jan 20 12:00:00 EST 2006”[0…11]

or

“Jan 20 12:00:00 EST 2006”[0…12] =\> Jan 20 12:00

And as a bonus, if you had used chop() instead of chomp(), your example  
would have worked, but that would be real bad! 🙂

Hope this helps in the future, for now, use the strftime like Ezra  
showed.

Cheers,

Bob S.

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [January 23, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/date-to-s-trimming/64298/4 "2006-01-23T17:50:22Z")

</div>

On 23-jan-2006, at 2:09, Mike S. wrote:

> When I convert a datetime to a string, it prints out something like  
> this
> 
> Jan 20 12:00:00 EST 2006
> 
> I want to trim off the last twelve characters, i.e.
> 
> :00 EST 2006

In config/environment.rb  
require\_dependency ‘datetime\_formats’

In lib/datetime\_formats.rb  
ActiveSupport::CoreExtensions::Time::Conversions::DATE\_FORMATS.merge!(  
:my\_date =\> ‘%b %d %I:%M’, # your format  
:long\_datetime =\> ‘%a %d-%m-%Y %H:%M’ # add more if needed  
)

In your view:  
\<%= object.method.to\_s(:my\_date) %\>

–  
Regards, Charles.
