2009-08-11 84 views
12

工作如何正確地只從日期時間對象中提取時間post.created_at,要準確)有/無的TimeZone的影響?如何提取daymonthyear的值?如何通過自定義模式提取day/month/year(有可能)?與日期時間對Rails

回答

30

時間字符串格式:

post.created_at.strftime("FORMAT STRING HERE") 
# Without the influence of time-zone: I take it that you want UTC 
post.created_at.utc.strftime("FORMAT STRING HERE") 

鏈接,strftime文檔: http://ruby-doc.org/core/classes/Time.html#method-i-strftime

對於越來越小時,分鐘和秒值:

post.created_at.hour 
post.created_at.min 
post.created_at.sec 
+0

拍攝,這正是我需要的!謝謝! – gmile 2009-08-11 13:11:14

+2

簡要說明,示例代碼和文檔鏈接。恆星的答案! – Mizipzor 2010-11-12 13:50:24

+0

@mizipzor:謝謝! – Swanand 2010-11-22 05:46:33

0
<%= l(@issue.created_at, format: :your_format) %> 

在現場YAML(app/config/locale/countr y_id.yml)您必須聲明

time: 
formats: 
    your_format: '%d %b. %Y' 
    your_another_format: '%I:%M' 

日期格式應爲便於配置您的i18n YAML定義文件中聲明,以及其他日期格式可以在這裏找到。