2011-12-22 65 views
2

我試圖返回一個日期作爲字符串,但從返回的小時中刪除第一個0。Ruby/Rails - 解析日期時間

這是我使用的代碼:

t = Time.new 

current_date = Time.local(t.year, t.month, t.day, t.hour, t.min/30*30) 
time_str = current_date.strftime("%I.%M+%p").downcase 

puts time_str 

Outputs: 
03.00+pm 

Expected output: 
3.00+pm # need to remove the last zero from the hour(s) 

回答

4

只使用%l代替%I

.strftime("%l.%M+%p") 
+0

由於返回的字符串是「3.00 + pm」,我不得不在末尾添加'strip!'。它的工作原理,謝謝! – fuzz 2011-12-22 04:22:40

+0

不客氣,不要忘記將它標記爲未來訪客幫助的接受答案。 – 2011-12-22 04:29:48

1

再補充一點:如果你想快速找到格式字符串,man date會如果你在OS X或Linux上,給你正確的答案。

如果strftimedate格式字符串不同,您可以隨時在Ruby文檔中找到它,這裏是document for strftime

+0

感謝您的提示。 – fuzz 2012-01-04 02:07:02