开发者

How can I compare a date field with date.today and display the differance?

I have a table of accounts, each with an expiration date. The e开发者_开发技巧xpiration date is saved in a date field (day, month and year).

How can I compare this to the current date and display the number of days until the expiration date is reached?

Thanks for any help it's much appreciated!


simple (Date.today - account.expiration_date).to_i will give you the integer number - the difference in days :)


You can convert your components into a target date and use some of the methods in DateHelper to display a human readable distance:

target_date = Date.new(model.year, model.month, model.day)

distance_of_time_in_words(target_date, Date.today)
=> "4 months"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜