开发者

Help with Ruby Date Compare

开发者 https://www.devze.com 2022-12-27 02:41 出处:网络
Yes, I\'ve read and done teh Google many t开发者_JS百科imes but I still can\'t get this working... maybe I\'m an idiot :)

Yes, I've read and done teh Google many t开发者_JS百科imes but I still can't get this working... maybe I'm an idiot :)

I have a system using tickets. Start date is "created_at" in the timestamps. Each ticket closes 7 days after "created_at". In the model, I'm using:

def closes
  (self.created_at + 7.days)
end

I'm trying to create another method that will take "closes" and return it as how many days, hours, minutes, and seconds are left before the ticket closes. Anyone want to help and/or admonish my skills? ;)

EDIT:

Ideally, I'd like the final output to be something like "6d14h22s" or something like that.


Use distance_of_time_in_words helper method:

class Ticket < ActiveRecord::Base
  include ActionView::Helpers::DateHelper

  def closes_in_words
    distance_of_time_in_words(Time.now, self.closes)
  end
end


I don't know of any good way to arbitrarily format durations with rails, but you can do it yourself like this:

def closes_in
  minutes = (Time.now - closes).to_i / 60
  days = minutes / (24*60)
  minutes -= days * 24*60
  hours = minutes / 60
  minutes -= hours * 60
  "#{days}d#{hours}h#{minutes}m"
end
0

精彩评论

暂无评论...
验证码 换一张
取 消