How to evaluate a date difference in years, months and days (ruby)?

后端 未结 5 1739
后悔当初
后悔当初 2021-01-04 03:06

I have to make a simple difference between two dates:

Date.parse(\"2009-06-20\") - Date.today

This gives me the difference of the dates in

5条回答
  •  滥情空心
    2021-01-04 03:55

    This is an example for difference in days, hours, seconds. Add the fields that you need.

    def calculate_difference
      minutes = (Date.parse("2009-06-20") - Date.today).to_i / 60
      days = minutes / (24*60)
      minutes -= days * 24*60
      hours = minutes / 60
      minutes -= hours * 60
      "#{days}d#{hours}h#{minutes}m"
    end
    

提交回复
热议问题