Caculate the time difference

marshluca posted @ Jul 27, 2009 05:26:45 AM in RoR with tags time ruby , 1485 阅读

 

require 'rubygems'
require 'date'
require 'time'

def format_time(input)
    now = DateTime.parse(Time.now.to_s)
    get = DateTime.parse(input.to_s)
    d = now - get
    hours,minutes,seconds,frac = DateTime.day_fraction_to_time(d)
    if hours.to_i > 24 * 30
      return "#{hours.to_i.div(24 * 30)}个月前"
    elsif hours.to_i > 24
      return "#{hours.to_i.div(24)}天前"
    elsif hours.to_i >= 1
      return "#{hours}小时#{minutes}分#{seconds}秒前"
    else
      return "#{minutes}分#{seconds}秒前."
    end
end

format("format_time("Tue Jul 21 11:15:03 +0800 2009")")

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter