返回顶部

收藏

Remaining days

更多
require 'date'
def remaining(date, event)
  intervals = [['day', 1], ['hour', 24], ['minute', 60], ['second', 60]]
  elapsed = DateTime.now - date
  tense = elapsed > 0 ? 'since' : 'until'
  interval = 1.0
  parts = intervals.collect do |name, new_interval|
    interval /= new_interval
    number, elapsed = elapsed.abs.divmod(interval)
  '#{number.to_i} #{name}#{'s' unless number == 1}'
  end
  puts '#{parts.join(', ')} #{tense} #{event}.'
end

remaining(DateTime.new(2006, 4, 15, 0, 0, 0, DateTime.now.offset),'the book deadline')

标签:Ruby,Date

收藏

0人收藏

支持

0

反对

0

发表评论