ruby 日期


difference = Time.now - time
seconds = difference % 60
difference = (difference - seconds) / 60
minutes = difference % 60
difference = (difference - minutes) / 60
hours = difference % 24
difference = (difference - hours) / 24
days = difference % 7
difference = (difference - days) / 7
weeks = difference % 4
difference = (difference - weeks) / 4
months = difference % 12
years = (difference - months) / 12

puts "(#{years} years , #{months} months ,#{weeks} weeks, #{days} days, #{hours}:#{minutes}:#{seconds})"



rail 实现


def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round

case distance_in_minutes
when 0..1
return (distance_in_minutes == 0) ? 'less than a minute' : '1 minute' unless include_seconds
case distance_in_seconds
when 0..4 then 'less than 5 seconds'
when 5..9 then 'less than 10 seconds'
when 10..19 then 'less than 20 seconds'
when 20..39 then 'half a minute'
when 40..59 then 'less than a minute'
else '1 minute'
end

when 2..44 then "#{distance_in_minutes} minutes"
when 45..89 then 'about 1 hour'
when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
when 1440..2879 then '1 day'
when 2880..43199 then "#{(distance_in_minutes / 1440).round} days"
when 43200..86399 then 'about 1 month'
when 86400..525599 then "#{(distance_in_minutes / 43200).round} months"
when 525600..1051199 then 'about 1 year'
else "over #{(distance_in_minutes / 525600).round} years"
end
end


中文实现:

#相对现在来格式化时间
def time_ago_in_words_zh(from_time, include_seconds = false)
distance_of_time_in_words(from_time,Time.now,include_seconds)
end

def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round

case distance_in_minutes
when 0..1
return ' 1 分钟' unless include_seconds
case distance_in_seconds
when 0..4 then ' 5 秒'
when 5..9 then ' 10 秒'
when 10..19 then ' 20 秒'
when 20..39 then ' 半分钟'
else ' 1 分钟'
end

when 2..44 then " #{distance_in_minutes} 分钟"
when 45..1439 then " #{(distance_in_minutes.to_f / 60.0).round} 小时"
when 1440..2879 then ' 昨天'
when 2880..4319 then ' 前天'
when 4320..43199 then " #{(distance_in_minutes / 1440).round} 天"
when 43200..525599 then " #{(distance_in_minutes / 43200).round} 个月"
else " #{(distance_in_minutes / 525600).round} 年"
end
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值