开发者

How to compare dates?

开发者 https://www.devze.com 2023-03-21 03:31 出处:网络
get_time_now= Time.now.strftime(\'%d/%m/%y\') question_deadline_time= (question.deadline.to_time - 2.days).strftime(\'%d/%m/%y\')
  get_time_now                = Time.now.strftime('%d/%m/%y') 
  question_deadline_time     = (question.deadline.to_time - 2.days).strftime('%d/%m/%y')


  if get_time_now 开发者_运维百科== question_deadline_time   #2 days till deadline
    Notifier.deliver_deadline_notification(inquiry, question, user, respondent , i)
  end

I need : If until deadline's date are remaining 2 DAYS so I deliver email. How i can do it?

UPD

when i write:

 deadline = question.deadline.midnight - 2.days


        if Time.now.midnight >= deadline

I get:

lib/scripts/deadline_notifier.rb:26: undefined method `midnight' for "19/07/11":String (NoMethodError)
    from lib/scripts/deadline_notifier.rb:18:in `each'
    from lib/scripts/deadline_notifier.rb:18

without midnight i get:

lib/scripts/deadline_notifier.rb:26: undefined method `-' for "19/07/11":String (NoMethodError)
    from lib/scripts/deadline_notifier.rb:18:in `each'
    from lib/scripts/deadline_notifier.rb:18


Use a combindation of .midnight (or .end_of_day) and 2.days to get what you want:

deadline = question.deadline.midnight - 2.days
if Time.now.midnight >= deadline
  #deliver
end

edited:

I highly recommend you change question.deadline to be a datetime. If you can't do that, then you need to convert your string to a date to perform calculations on it. @floor's method works fine, or you can do this as well:

"2011-07-18".to_date


From the error it looks like you are trying to run a DateTime method on a string. If you have a DateTime object and run a strftime('%d/%m/%y') on it, you can't call DateTime methods any more because it is no longer an object, just a plain ol' string. So you can't run midnight or use the subtract operand.

Also, what format is the string that you are storing? You can try casting it with "date string".to_date, then running your methods on it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号