Subtract a number from a date in Ruby on Rails

2

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

example:

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
source

1 answer

2

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

dias = 5
fecha = Date.new(2017,06,02)
fecha_resultado = fecha - 5

This of course is also useful for ActiveRecord

    
answered by 07.07.2017 / 16:03
source