# String weirdness

**URL:** <https://www.ruby-forum.com/t/string-weirdness/47661>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/string-weirdness/47661 "2005-11-12T12:50:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/string-weirdness/47661/1 "2005-11-12T12:50:00Z")

</div>

I’m a newbie so please have pity if the answer is obvioius 😉

I wrote a date formatter. It works as expected in irb, but if i try to  
use  
it to format a date for display as the value of an input (type=text)  
field,  
it truncates unexpectedly.

This is the formatter:

def format\_date\_short\_us(date)  
month\_hash = Hash[1 =\>‘Jan’, 2 =\> ‘Feb’, 3=\>,…etc… 12 =\> ‘Dec’]  
“#{month\_hash[adate.month]} #{adate.day}, #{adate.year}”  
end

Within IRB, passing in Date.today returns:  
=\> “Nov 11, 2005”

The html result using Date.today is:  
“Nov”

This is the invocation from html:

\<input type=“text”  
name=“st\_date”  
id=“f\_date\_c”  
value=\<%= format\_date\_short\_us(@project.st\_date) %\> /\>

Any help is greatly appreciated. thanks.

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/string-weirdness/47661/2 "2005-11-12T12:50:00Z")

</div>

note the quotes around the value

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/string-weirdness/47661/3 "2005-11-12T12:50:00Z")

</div>

Hi newbie,

I have a helper that is doing the same thing. Firstly, your value=  
attribute doesn’t have “” around the actual value data (the date).

btw, I’d make sure you have an object that can understand date methods  
before throwing them around, if you gave a string to your function it  
wouldn’t act very happy. And you might want to use  
Date::ABBR\_MONTHNAMES, which already has the abbreviations, and will not  
need to be instantiated every time you use that method.

-Matt B

def d(unformatted)  
if (unformatted.kind\_of?(Date) || unformatted.kind\_of?(Time))  
“#{Date::ABBR\_MONTHNAMES[unformatted.mon]} #{unformatted.mday},  
#{unformatted.year}”  
end  
end

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/string-weirdness/47661/4 "2005-11-12T12:50:00Z")

</div>

On Nov 11, 2005, at 8:32 AM, Larry W. wrote:

> ‘Dec’]  
> “#{month\_hash[adate.month]} #{adate.day}, #{adate.year}”  
> end

It looks to me like you’re using Date/Time objects. If so, just do:

date.strftime(’%b %d, %Y’)
