simo
simo

Reputation: 24570

How to check time left until a specific date?

How do I check how many days left until a specific date?

For example, I want to start executing some logic after 3 days from today? I will be hard coding today's date, and subtract now from it, but how?

EDIT

What I want to do is to disable devise confirmation email at model for the next three days,

if Rails.env== 'production' && (three days left sense 1/7/2013)
   devise :confirmable
end

Upvotes: 2

Views: 958

Answers (5)

Eugene Rourke
Eugene Rourke

Reputation: 4934

why bother?

if Rails.env== 'production' && Time.now.strftime("%Y%m%d").to_i >= 20130110
    devise :confirmable 
end

Upvotes: 1

shweta
shweta

Reputation: 8169

try

if Time.now >= <your_time_object> + 3.days
  # start executing some logic
end

Upvotes: 0

Abdulrahman_88
Abdulrahman_88

Reputation: 560

in your class first you have to set date e.g:

in your case you need to do something like this:

  ("object name".date.to_time<=time.now+3)
   (do something)if x.date.to_time<=today

the technique to_time make`s you able to choose time (day,weeks,month,year)

ENV.all.each do|env|
  where (env.date.to_time<= Time.now+3)
   if Rails.env== 'production' && (env.date.to_time== today)
      devise :confirmable
   end

Upvotes: 0

sjain
sjain

Reputation: 23354

Use the time_diff gem to get the difference in terms of year, month, week, day, hour, minute and second that can be easily achieved as I did so.

Check this - Rails calculate time difference

You will get your answer there.

Upvotes: 0

MichaelR
MichaelR

Reputation: 999

If im getting it right ,

you just store the start time in a variable then check it against the current time (you'll get the diff in seconds ) and if its larger then 72(3 days) the if statement will be done.

require 'time'
start_time=Time.now

if Rails.env== 'production' && ( (Time.new - start_time)/3600 >72 )
   devise :confirmable
end

Upvotes: 0

Related Questions