Caculate the time difference

marshluca posted @ 16 年前 in RoR with tags time ruby , 1619 阅读

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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