ruby on rails 方法小结

1.获取两个日期之间的月份和年份的唯一数组

1
2
3
4
5
6
7
8
9
10
11
12
13
require 'date'
 
def doit(first, last)
  first = first << 1
  (12*last.year + last.month - 12*first.year - first.month + 1).
    times.map { |i| (first = first >> 1).strftime("%b %Y") }
end
 
first = Date.parse('Fri, 02 Dec 2016')
last  = Date.parse('Wed, 01 Mar 2017')
 
doit(first, last)
#=> ["Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017"]

  

2.rails时间戳,查看自上次以来经过了多长时间

1
time_ago_in_words @object.created_at

如果你想使用它的控制檯用它玩,請確保你的前言是helper,所以它被加載到控制檯中

1
helper.time_ago_in_words @object.created_at

对于time_ago_in_words 拓展:

1
2
3
4
5
6
7
8
9
10
11
time_ago_in_words(3.minutes.from_now)                 # => 3 minutes
time_ago_in_words(3.minutes.ago)                      # => 3 minutes
time_ago_in_words(Time.now - 15.hours)                # => about 15 hours
time_ago_in_words(Time.now)                           # => less than a minute
time_ago_in_words(Time.now, include_seconds: true) # => less than 5 seconds
 
from_time = Time.now - 3.days - 14.minutes - 25.seconds
time_ago_in_words(from_time)      # => 3 days
 
from_time = (3.days + 14.minutes + 25.seconds).ago
time_ago_in_words(from_time)      # => 3 days

  

爲了檢驗日期2之間,距離現在不只是一個日期,然後就可以用distance_of_time_in_words

1
distance_of_time_in_words(@object.created_at, @object.updated_at) eg:### => 12天前 

对于 distance_of_time_in_words 这个方法的拓展:

复制代码
0 <-> 29 secs                                                             # => less than a minute
30 secs <-> 1 min, 29 secs                                                # => 1 minute
1 min, 30 secs <-> 44 mins, 29 secs                                       # => [2..44] minutes
44 mins, 30 secs <-> 89 mins, 29 secs                                     # => about 1 hour
89 mins, 30 secs <-> 23 hrs, 59 mins, 29 secs                             # => about [2..24] hours
23 hrs, 59 mins, 30 secs <-> 41 hrs, 59 mins, 29 secs                     # => 1 day
41 hrs, 59 mins, 30 secs  <-> 29 days, 23 hrs, 59 mins, 29 secs           # => [2..29] days
29 days, 23 hrs, 59 mins, 30 secs <-> 44 days, 23 hrs, 59 mins, 29 secs   # => about 1 month
44 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs   # => about 2 months
59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec                    # => [2..12] months
1 yr <-> 1 yr, 3 months                                                   # => about 1 year
1 yr, 3 months <-> 1 yr, 9 months                                         # => over 1 year
1 yr, 9 months <-> 2 yr minus 1 sec                                       # => almost 2 years
2 yrs <-> max time or date                                                # => (same rules as 1 yr)


0-4   secs      # => less than 5 seconds
5-9   secs      # => less than 10 seconds
10-19 secs      # => less than 20 seconds
20-39 secs      # => half a minute
40-59 secs      # => less than a minute
60-89 secs      # => 1 minute

from_time = Time.now
distance_of_time_in_words(from_time, from_time + 50.minutes)                                # => about 1 hour
distance_of_time_in_words(from_time, 50.minutes.from_now)                                   # => about 1 hour
distance_of_time_in_words(from_time, from_time + 15.seconds)                                # => less than a minute
distance_of_time_in_words(from_time, from_time + 15.seconds, include_seconds: true)         # => less than 20 seconds
distance_of_time_in_words(from_time, 3.years.from_now)                                      # => about 3 years
distance_of_time_in_words(from_time, from_time + 60.hours)                                  # => 3 days
distance_of_time_in_words(from_time, from_time + 45.seconds, include_seconds: true)         # => less than a minute
distance_of_time_in_words(from_time, from_time - 45.seconds, include_seconds: true)         # => less than a minute
distance_of_time_in_words(from_time, 76.seconds.from_now)                                   # => 1 minute
distance_of_time_in_words(from_time, from_time + 1.year + 3.days)                           # => about 1 year
distance_of_time_in_words(from_time, from_time + 3.years + 6.months)                        # => over 3 years
distance_of_time_in_words(from_time, from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => about 4 years

to_time = Time.now + 6.years + 19.days
distance_of_time_in_words(from_time, to_time, include_seconds: true)                        # => about 6 years
distance_of_time_in_words(to_time, from_time, include_seconds: true)                        # => about 6 years
distance_of_time_in_words(Time.now, Time.now)                                               # => less than a minute


#### I18n.t
datetime:
  distance_in_words:
    short:
      about_x_hours:
        one: 'an hour'
        other: '%{count} hours'

from_time = Time.now
distance_of_time_in_words(from_time, from_time + 50.minutes, scope: 'datetime.distance_in_words.short') # => "an hour"
distance_of_time_in_words(from_time, from_time + 3.hours, scope: 'datetime.distance_in_words.short')    # => "3 hours"
 
复制代码

 

 

 

posted @   鞋带松了  阅读(69)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示