In Rails, display time between two dates in English

前端 未结 6 550
南方客
南方客 2020-12-07 19:15

In a Rails project I want to find the difference between two dates and then display it in natural language. Something like

>> (date1 - date2).to_natur         


        
6条回答
  •  星月不相逢
    2020-12-07 19:55

    distance_of_time_in_words is the most accurate here. Daniel's answer is actully wrong: 2.5 years ago should produce exactly 2 years, 6 months. The issue is that months contain 28-31 day, and years might be leap.

    I wish I knew how to fix this :(

提交回复
热议问题