Execute a block depending on timestamps in Ruby

0

I want a block of code to be executed, with the following condition:

In a variable I have saved a timestamp, ex. @tiempo date: 2018-06-11 18:04:25 -0500, and the current time is # => 2018-06-13 18:04:25 -0500

If the difference between the variable @tiempo and the current time is 2 days, execute the following code, if not, execute another block of code, something like this:

if @tiempo.days_ago(2).from.now
  # ejecutar un bloque de código
else
  # otro código
end

How could I do it?

Thank you.

    
asked by Carlos Gómez 14.06.2018 в 01:34
source

1 answer

1

You can subtract the dates and compare against the result (which will be in seconds); for example:

if Time.now - @tiempo >= 172_800
  # ejecutar un bloque de código
else
  # otro código
end
  • 172_800 is the total seconds in two days (60 seconds * 60 minutes * 24 hours * 2 days).
answered by 14.06.2018 / 05:09
source