看了几个人提供的nice date time方法,不怎么样,不准确。包括javaeye在内,明明是前天,时间竟写成昨天。
自己改了一个。
# 精彩(nice)时间格式化
def status_time_ago_in_words(dt)
time = time_ago_in_words(dt)
case time
when "less than a minute"
"刚刚"
when /minute/
time.gsub(/minutes|minute/, '分钟') + "前"
when /hour|days|day/
if dt.to_date == Time.now.to_date then
time.gsub(/about (\d+) (hours|hour)/, '\1小时') + "前"
elsif dt.to_date == Time.now.yesterday.to_date then
"昨天#{dt.strftime(' %H:%M')}"
elsif dt.to_date == Time.now.ago(172800).to_date then
"前天#{dt.strftime(' %H:%M')}"
else
dt.strftime("%Y-%m-%d %H:%M")
end
else
dt.strftime("%Y-%m-%d %H:%M")
end
end