红宝石日期减法

发布于 2024-10-28 06:34:51 字数 373 浏览 1 评论 0原文

我在 Ruby 中遇到日期减法问题,

"2011-03-29".to_date - "2011-03-20".to_date #=> (9/1)
("2011-03-29".to_date - "2011-03-20".to_date).to_i #=> 9

似乎它返回日期之间的天数差异。

日期差的年数、月数、天数,

ie ("2011-03-29".to_date - "2011-03-20".to_date)

现在我的问题是返回

0 years, 0 month and 9 days

谢谢。

I have a problem with date subtraction in Ruby

"2011-03-29".to_date - "2011-03-20".to_date #=> (9/1)
("2011-03-29".to_date - "2011-03-20".to_date).to_i #=> 9

Seems it's returning the difference between dates in number of days.

Now my problem is to return number of years, months, days of the date difference

ie ("2011-03-29".to_date - "2011-03-20".to_date)

should return

0 years, 0 month and 9 days

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-11-04 06:34:51

您可以尝试此链接:

http://api. rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words

  def time_diff_in_natural_language(from_time, to_time)
    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_seconds = ((to_time - from_time).abs).round
    components = []

    %w(year month week day).each do |interval|
      # For each interval type, if the amount of time remaining is greater than
      # one unit, calculate how many units fit into the remaining time.
      if distance_in_seconds >= 1.send(interval)
        delta = (distance_in_seconds / 1.send(interval)).floor
        distance_in_seconds -= delta.send(interval)
        components << pluralize(delta, interval)
      end
    end

    components.join(", ")
  end

  time_diff_in_natural_language(Time.now, 2.5.years.ago)
  >> 2 years, 6 months, 2 days

参考:在 Rails 中,用英语显示两个日期之间的时间

You can try this link:

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words

OR

  def time_diff_in_natural_language(from_time, to_time)
    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_seconds = ((to_time - from_time).abs).round
    components = []

    %w(year month week day).each do |interval|
      # For each interval type, if the amount of time remaining is greater than
      # one unit, calculate how many units fit into the remaining time.
      if distance_in_seconds >= 1.send(interval)
        delta = (distance_in_seconds / 1.send(interval)).floor
        distance_in_seconds -= delta.send(interval)
        components << pluralize(delta, interval)
      end
    end

    components.join(", ")
  end

  time_diff_in_natural_language(Time.now, 2.5.years.ago)
  >> 2 years, 6 months, 2 days

Reference: In Rails, display time between two dates in English

俏︾媚 2024-11-04 06:34:51

我知道它有点脏,但你尝试过吗:

result = Date.new(0) + ("2011-03-29".to_date - "2011-03-20".to_date)
puts "#{result.year} years, #{result.month - 1} months and #{result.day} days"

I know it is kinda dirty but have you tried:

result = Date.new(0) + ("2011-03-29".to_date - "2011-03-20".to_date)
puts "#{result.year} years, #{result.month - 1} months and #{result.day} days"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文