Subtract a number from a date in Ruby on Rails


How can I subtract a variable from a date in Ruby on Rails,


I want to subtract a variable, which can contain any value between 1 and 7, which represent the days, at a specific date.

in algorithm it would be something like this:

días = 5
fecha = 20170602
fecha_resultado = fecha - días

date_result would have the value of: 20170527

asked by rrg1459 07.07.2017 в 15:40

1 answer


You can if you use the type Date that Ruby implements for these cases:

dias = 5
fecha =,06,02)
fecha_resultado = fecha - 5

This of course is also useful for ActiveRecord

answered by 07.07.2017 / 16:03