2008
-
06
-
25
ruby 日期
关键字: ruby time- 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
发表评论
表情图标
![](https://i-blog.csdnimg.cn/blog_migrate/fdae2ee6389663d0e8e8cedf36783999.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4762a8ec9eb60433747467059e99f95e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/70e6f45409acf447b2fa7efdb0a3e40d.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0bb8607afda370c63b5554b6e8885ffd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/395f56c5980fbb3e31f1ec85cb557b9e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4dcbb665af73b7d0cf235c3bf4436f90.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e62600a0b5762b94396c35c27b2adda8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d452feb9f9bd26ce6184b2f5623f1534.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/af6d31ea11d11d9ab3266033918d6c46.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0fdad3ca8964cd28ec8625ec3ccd4cfb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0148983d7112d60e2c6749bfa31e8e6.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a038f8d127ac958f0fc9162c1828d461.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/48463b748ef69aacec3dc9cc457c7d87.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/bdd2d55736d2015fe49de71c9e3202fe.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4549eaaee5401b5a90f205c2b76a7e1f.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/27adcb58c659b2eb3007508809b3ce32.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/5a8c29aae2babf927bb7b1b173be274d.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3c1dd00b897b54388a49dea885d8a014.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e764c68af0f4115017332777a9af276c.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6fbac1a3fbe39ee7868ad025050f1598.gif)
字体颜色: 标准深红红色橙色棕色黄色绿色橄榄青色蓝色深蓝靛蓝紫色灰色白色黑色 字体大小: 标准1 (xx-small)2 (x-small)3 (small)4 (medium)5 (large)6 (x-large)7 (xx-large) 对齐: 标准居左居中居右
提示:选择您需要装饰的文字, 按上列按钮即可添加上相应的标签
您还没有登录,请登录后发表评论(快捷键 Alt+S / Ctrl+Enter)
评论