开发者

Rails - Include a Date/Time Stamp in a string on create

in my def create controller I'd 开发者_JS百科like to include something along the lines of:

:description = > 'Uploaded on May 27, 2010"

How can I make the May 27, 2010 dynamic in the controller?

Thanks!


Try this!

:description => "Uploaded on #{Date.today.strftime("%b %d,%Y")}"


I assume when you say you want to make May 27, 2010 then you mean you want to set the current date. For that you need to use:

:description = > "Uploaded on " + Time.now.to_s

Output will be:

"Uploaded on Fri Oct 22 21:48:49 -0700 2010"

There are many options in the Time class which you can use. This is an interesting read for timezone consideration for Rails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜