开发者

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

开发者 https://www.devze.com 2023-01-20 21:08 出处:网络
in my def create controller I\'d 开发者_JS百科like to include something along the lines of: :description = > \'Uploaded on May 27, 2010\"

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

精彩评论

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