ruby 日期 好函数

2008-06-25

ruby 日期

关键字: ruby time
Ruby代码 
  1. difference = Time.now - time  
  2.     seconds    =  difference % 60  
  3.     difference = (difference - seconds) / 60  
  4.     minutes    =  difference % 60  
  5.     difference = (difference - minutes) / 60  
  6.     hours      =  difference % 24  
  7.     difference = (difference - hours)   / 24  
  8.     days       =  difference % 7  
  9.     difference = (difference - days)    /  7  
  10.     weeks      =  difference % 4  
  11.     difference = (difference - weeks)   / 4  
  12.     months     =  difference % 12  
  13.     years      = (difference - months)  / 12  
  14.   
  15. puts "(#{years} years , #{months} months ,#{weeks} weeks, #{days} days, #{hours}:#{minutes}:#{seconds})"  



rail 实现 

Ruby代码 
  1. def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)  
  2.         from_time = from_time.to_time if from_time.respond_to?(:to_time)  
  3.         to_time = to_time.to_time if to_time.respond_to?(:to_time)  
  4.         distance_in_minutes = (((to_time - from_time).abs)/60).round  
  5.         distance_in_seconds = ((to_time - from_time).abs).round  
  6.   
  7.         case distance_in_minutes  
  8.           when 0..1  
  9.             return (distance_in_minutes == 0) ? 'less than a minute' : '1 minute' unless include_seconds  
  10.             case distance_in_seconds  
  11.               when 0..4   then 'less than 5 seconds'  
  12.               when 5..9   then 'less than 10 seconds'  
  13.               when 10..19 then 'less than 20 seconds'  
  14.               when 20..39 then 'half a minute'  
  15.               when 40..59 then 'less than a minute'  
  16.               else             '1 minute'  
  17.             end  
  18.   
  19.           when 2..44           then "#{distance_in_minutes} minutes"  
  20.           when 45..89          then 'about 1 hour'  
  21.           when 90..1439        then "about #{(distance_in_minutes.to_f / 60.0).round} hours"  
  22.           when 1440..2879      then '1 day'  
  23.           when 2880..43199     then "#{(distance_in_minutes / 1440).round} days"  
  24.           when 43200..86399    then 'about 1 month'  
  25.           when 86400..525599   then "#{(distance_in_minutes / 43200).round} months"  
  26.           when 525600..1051199 then 'about 1 year'  
  27.           else                      "over #{(distance_in_minutes / 525600).round} years"  
  28.         end  
  29.       end  


中文实现: 
Ruby代码 
  1. #相对现在来格式化时间  
  2. def time_ago_in_words_zh(from_time, include_seconds = false)  
  3.   distance_of_time_in_words(from_time,Time.now,include_seconds)  
  4. end  
  5.   
  6. def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)  
  7.   from_time = from_time.to_time if from_time.respond_to?(:to_time)  
  8.   to_time = to_time.to_time if to_time.respond_to?(:to_time)  
  9.   distance_in_minutes = (((to_time - from_time).abs)/60).round  
  10.   distance_in_seconds = ((to_time - from_time).abs).round  
  11.   
  12.   case distance_in_minutes  
  13.     when 0..1  
  14.       return ' 1 分钟' unless include_seconds  
  15.       case distance_in_seconds  
  16.         when 0..4   then ' 5 秒'  
  17.         when 5..9   then ' 10 秒'  
  18.         when 10..19 then ' 20 秒'  
  19.         when 20..39 then ' 半分钟'  
  20.         else             ' 1 分钟'  
  21.       end  
  22.   
  23.     when 2..44           then " #{distance_in_minutes} 分钟"  
  24.     when 45..1439        then " #{(distance_in_minutes.to_f / 60.0).round} 小时"  
  25.     when 1440..2879      then ' 昨天'  
  26.     when 2880..4319      then ' 前天'  
  27.     when 4320..43199     then " #{(distance_in_minutes / 1440).round} 天"  
  28.     when 43200..525599   then " #{(distance_in_minutes / 43200).round} 个月"  
  29.     else                      " #{(distance_in_minutes / 525600).round} 年"  
  30.   end  
  31. end  
评论
发表评论
表情图标

字体颜色:  字体大小:  对齐: 
提示:选择您需要装饰的文字, 按上列按钮即可添加上相应的标签

您还没有登录,请登录后发表评论(快捷键 Alt+S / Ctrl+Enter) 

原文地址:https://www.cnblogs.com/lexus/p/1888343.html