Hey guys, complete rubynooblet q here: I have a ''created_at'' field for a model, which, by default, prints out to: "2 Mon Mar 28 16:13:41 GMT 2005" (hooray for automagical setting by RoR!) What''s the best way in RoR to format that time into something shorter? I know that you can use <%= modelname.created_at_before_type_cast %>, which will give me ''2005-03-28 16:13:41'', but what if I''m after more flexibility? Looking at the ruby refdocs, I don''t see how you can instantiate a Time object from a String. Am I blind/stupid/worth-rejecting-from-support-cast-of-deliverance? Must I use <%= modelname.created_at_before_type_cast[11,8] %> to get ''16:13:41'' ? -scott (digest user, so cc''s welcomed)
(originally sent from wrong email account, sending again..) Hey guys, complete rubynooblet q here: I have a ''created_at'' field for a model, which, by default, prints out to: "2 Mon Mar 28 16:13:41 GMT 2005" (hooray for automagical setting by RoR!) What''s the best way in RoR to format that time into something shorter? I know that you can use <%= modelname.created_at_before_type_cast %>, which will give me ''2005-03-28 16:13:41'', but what if I''m after more flexibility? Looking at the ruby refdocs, I don''t see how you can instantiate a Time object from a String. Am I blind/stupid/worth-rejecting-from-support-cast-of-deliverance? Must I use <%= modelname.created_at_before_type_cast[11,8] %> to get ''16:13:41'' ? -scott (digest user, so cc''s welcomed)
Scott-
Since it''s just a Time object, the easiest way would be:
modelname.created_at.strftime("%H:%M:%S")
See: http://www.ruby-doc.org/docs/rdoc/1.9/classes/Time.html#M000232
Cheers,
Ben
On Mon, 28 Mar 2005 17:03:17 +0000, Scott
<scott-NaGkl6Xq47/R7s880joybQ@public.gmane.org>
wrote:> Hey guys,
>
> complete rubynooblet q here:
>
> I have a ''created_at'' field for a model, which, by
default, prints out
> to:
>
> "2 Mon Mar 28 16:13:41 GMT 2005" (hooray for automagical
setting by
> RoR!)
>
> What''s the best way in RoR to format that time into something
shorter?
> I know that you can use <%= modelname.created_at_before_type_cast %>,
> which will give me ''2005-03-28 16:13:41'', but what if
I''m after more
> flexibility?
>
> Looking at the ruby refdocs, I don''t see how you can instantiate a
Time
> object from a String. Am I
> blind/stupid/worth-rejecting-from-support-cast-of-deliverance?
>
> Must I use <%= modelname.created_at_before_type_cast[11,8] %> to get
> ''16:13:41'' ?
>
> -scott
> (digest user, so cc''s welcomed)
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>