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. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi newbie, I have a helper that is doing the same thing. Firstly, your valueattribute 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 On Fri, 2005-11-11 at 08:32 -0500, Larry White wrote:> 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. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
<input type="text" name="st_date" id="f_date_c" value="<%= format_date_short_us(@project.st_date) %>" /> note the quotes around the value> > <input type="text" > name="st_date" > id="f_date_c" > value=<%= format_date_short_us(@project.st_date) %> /> > > >------------------------------------------------------------------------ > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >
On Nov 11, 2005, at 8:32 AM, Larry White wrote:> 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}" > endIt looks to me like you''re using Date/Time objects. If so, just do: date.strftime(''%b %d, %Y'') -- Scott Barron Lunchbox Software http://lunchboxsoftware.com http://lunchroom.lunchboxsoftware.com http://rubyi.st