ruby 日期 好函数
徐构
永久域名 http://hooney.javaeye.com
rails route | 使用include中嵌Hash取出一个多层次的对象 ...
2008 - 06 - 25
ruby 日期 关键字: ruby time
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 实现
Ruby代码
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中文实现:
Ruby代码
#相对现在来格式化时间 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 endrails route | 使用include中嵌Hash取出一个多层次的对象 ...
12:38 浏览 (685) 评论 (0) 分类: ruby 相关推荐
评论
发表评论
表情图标
字体颜色: 标准 深红 红色 橙色 棕色 黄色 绿色 橄榄 青色 蓝色 深蓝 靛蓝 紫色 灰色 白色 黑色 字体大小: 标准 1 (xx-small) 2 (x-small) 3 (small) 4 (medium) 5 (large) 6 (x-large) 7 (xx-large) 对齐: 标准 居左 居中 居右
提示:选择您需要装饰的文字, 按上列按钮即可添加上相应的标签
您还没有登录,请 登录 后发表评论(快捷键 Alt+S / Ctrl+Enter)
xu_wccq
浏览: 34598 次 性别: 来自: 深圳 详细资料 留言簿 搜索本博客
最近访客 >>更多访客
其他分类 我的收藏 (8) 我的论坛主题贴 (3) 我的所有论坛贴 (17) 我的精华良好贴 (0)
最近加入圈子 深圳开发圈 CSS探讨
存档 2010-05 (2) 2009-11 (2) 2009-06 (2) 更多存档...
评论排行榜 htc diamond2 玩 rockbank(吉它英雄) skyfire Sorry. Skyfire Cannot Provide Se ...
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。若作者同意转载,必须以超链接形式标明文章原始出处和作者。
© 2003-2010 JavaEye.com. All rights reserved. 上海炯耐计算机软件有限公司 [ 沪ICP备05023328号 ]