Blankman
Blankman

Reputation: 266960

How to test if the a model datetime is in the past?

I have a model with a datetime (postgresql) and I want to test if the datetime has expired.

Content
-expired_at datetime

I have a method I am trying to write in my Content model:

def expired?
  ??? > expired_at
end

How should I be getting the current date and seeing if it greater than the current date and time.

Upvotes: 0

Views: 190

Answers (1)

MarsAtomic
MarsAtomic

Reputation: 10673

If you have expired_at as a Ruby DateTime, you can do something like this:

expiration = DateTime.now

if expired_at < expiration
  # not expired
else
  # meets or exceeds expiration time
end

If you're getting string data from the database, you can use:

DateTime.parse(my_date_string)

to create a DateTime object.

Upvotes: 1

Related Questions