0

If I have @time = Time.now.strftime("%Y-%m-%d %H:%M:%S"),

How can I reduce this time by 15 minutes ?

I already tried this one :: @reducetime = @time-15.minutes, works fine at console but give errors while execution. Other than this Is there any way to resolve this issue.

Thanks

Rubyist
  • 6,486
  • 10
  • 51
  • 86
  • 3
    Why are you formatting the time before trying to subtract 15 minutes from it? Your `@time` is a string by the time you try to compute `@reducetime`. – mu is too short May 24 '11 at 07:46

1 Answers1

3

Your problem is that you're formatting your time into a string before you're done treating it as a time. This would make more sense:

@time       = Time.now
@reducetime = @time - 15.minutes

# And then later when you're reading to display @time...
formatted_time = @time.strftime("%Y-%m-%d %H:%M:%S")

You shouldn't format your data until right before you're ready to display it.

If you must have @time as the formatted time then you're going to have to parse it before computing @reducetime:

@reducetime = (DateTime.strptime(@time, "%Y-%m-%d %H:%M:%S") - 15.minutes).to_time
mu is too short
  • 426,620
  • 70
  • 833
  • 800