How to show an hour later from now or an hour before in ruby

x = Time.now
y = 1.hours.from_now
z = 1.hours.before_now

Sorry if this question is too stupid. I want to know an effective way to show one hour before and after in ruby. Thanks for watching and help.

+4
source share
2 answers

There are no such methods in Ruby. You will need to do:

Time.now + 1*60*60 # 1.hours.from_now -> 1 hour, 60 minutes, 60 seconds
Time.now - 1*60*60 # 1.hours.before_now -> 1 hour, 60 minutes, 60 seconds

or you can include activesupportin your project.

+5
source

You can also write as follows:

Current time:

x = Time.now => 2017-02-14 11:55:17 +0530

an hour ago (an hour before):

y = x - 1.hour => 2017-02-14 10:55:17 +0530

or

y = 1.hour.ago => 2017-02-14 10:55:17 +0530 `# available in rails v4.2

in an hour / in (in an hour after):

z = x + 1.hour => 2017-02-14 12:55:17 +0530

or

z = 1.hour.since => 2017-02-14 12:55:17 +0530 # available in rails v4.2

: x ( ) (Time.now), , (Time.now)

0

Source: https://habr.com/ru/post/1669657/


All Articles