2011-12-22 57 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