记忆中好像是在哪里看到过这样一个插件,但当时没有关注,现在有些记不起来了!请问有没有人能帮忙解答下!谢谢!
来个修改版,用这个吧,总不能显示多少天吧,如果太久了,显示成直接的时间好一些。
[code="ruby"]
#JavaEye的时候格式
def status_time_ago_in_words(time)
time_word = time_ago_in_words(time)
case time_word
when "less than a minute"
"刚刚"
when /minute/
time_word.gsub(/minutes|minute/,'分钟') + "前"
when /hour/
##time.gsub(/about/,'').gsub(/hours/,'小时') + "前"
time_word.gsub(/about (\d+) (hours|hour)/, '\1小时') + "前后"
when "1 day"
"昨天"
when "2 days"
"前天"
when "3 days"
"3天前"
else
time.strftime("%Y年%m月%d %H:%M")
end
end
[/code]
记得我答过这个问题
http://www.iteye.com/problems/2294
写在helper中:
[quote]
def get_time(time)
if !time
return ""
elsif time > 10.seconds.ago
return "刚刚"
elsif time > 1.minutes.ago
return "1分钟前"
elsif time > 2.minutes.ago
return "2分钟前"
elsif time > 3.minutes.ago
return "3分钟前"
elsif time > 4.minutes.ago
return "4分钟前"
elsif time > 5.minutes.ago
return "5分钟前"
elsif time > 6.minutes.ago
return "6分钟前"
elsif time > 7.minutes.ago
return "7分钟前"
elsif time > 8.minutes.ago
return "8分钟前"
elsif time > 9.minutes.ago
return "9分钟前"
elsif time > 10.minutes.ago
return "10分钟前"
else
return time.strftime("%Y-%m-%d %H:%M")
end
end
[/quote]
这个我早就写好了:
[code="ruby"]
def status_time_ago_in_words(time)
time = time_ago_in_words(time)
case time
when "less than a minute"
"刚刚"
when /minute/
time.gsub(/minute|minutes/,'分钟') + "前"
when /hour/
##time.gsub(/about/,'').gsub(/hours/,'小时') + "前"
time.gsub(/about (\d+) (hour|hours)/, '\1小时') + "前后"
when "1 day"
"昨天"
when "2 days"
"前天"
else
time.gsub(/days/,'天') + "前"
end
end
[/code]
把它放在helper里,然后在view中直接用。
哈哈,楼上写的代码不好看。