Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of Time.now.advance() in Ruby

http://api.rubyonrails.org/classes/Time.html#method-i-advance documents a method advance() to add to the current time. Is there an opposite, 'go_back() method?

like image 921
user94154 Avatar asked Aug 17 '09 20:08

user94154


People also ask

What is Time now in Ruby?

The now() is an inbuilt method in Ruby returns the current time. Syntax: time.now() Parameters: The function accepts no parameter. Return Value: It returns the current time.

How do I get the current date in Ruby?

You can get the current date using Date. today .

What is date in Ruby?

Ruby has three components for representing Date and Time class. Date – Date class manages the dates, day, month, year. Time – It manages the seconds, minute, hours. DateTime – It includes the above two classes.


2 Answers

I don't think there is but there's nothing to stop you using negative values in advance.

Time.now.advance(:days => - 1, :hours => -1)
like image 161
Eifion Avatar answered Oct 17 '22 17:10

Eifion


You can use something like the following example

Time.now - 3.days
like image 22
tliff Avatar answered Oct 17 '22 18:10

tliff