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.
Related
I'm attempting to convert a string date into a date that can be stored in the database. These are my attempts:
params[:event][:start_date] = '03-21-2016'
DateTime.strptime(params[:event][:start_date], '%Y-%m-%dT%H:%M:%S%z')
or
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y')
However I keep getting an invalid date error. I'm not sure what I'm doing wrong.
One way to do it would be this:
date_string = '03-21-2016'
month, day, year = date_string.split('-').map(&:to_i)
DateTime.new(year, month, day)
You need to understand what each fragment (eg %Y) in the format string ('%Y-%m-%dT%H:%M:%S%z') means: read this.
http://apidock.com/rails/ActiveSupport/TimeWithZone/strftime
Once you know this, you can tailor a format string to the date string you have, or expect to get: in this case, "%m-%d-%Y".
When debugging create a new, basic and simple, version of the code and test that.
require 'date'
params = '03-21-2016'
DateTime.strptime(params, '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>
Note the order for the format: '%m-%d-%Y', which works. That's the problem with your first attempt, where you tried to use%Y-%m-%d. There is NO month21or day2016`.
Your second attempt is valid but your question makes it appear it doesn't work. You need to be more careful with your testing:
params = {event:{start_date:'03-21-2016'}}
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>
I'm trying to subtract the earliest date from two arrays:
def days_pending
start = [object.created_at]
finish = [Time.now.strftime('%Y-%m-%d')]
if object.app_sign_date
start.push(object.app_sign_date)
end
if object.submitted_date
start.push(object.submitted_date)
end
if object.inforce_date
finish.push(object.inforce_date)
end
if object.closed_date
finish.push(object.closed_date)
end
finish.min - start.min
I have no problem calling min on the arrays, but have a problem calling the min method and then subtracting. I get NoMethodError: undefined method-' for "2015-01-01":String`.
the array finish has first element as string not the date. you need to add appropriate object instead ie. date or datetime
finish = [Time.now]
or
finish = [DateTime.now]
After calling Time.now.strftime('%Y-%m-%d'), what you got is a string.
Change to Time.now should work.
If you need the output to be in the form "2015-01-01" you could use parse:
Time.parse(finish.min) - start.min
I have the code below in my routes.rb file:
get 'page/contact_us(/:year(/:month))'=>'page#contact_us', :as => 'contact_us'
The idea is that entering year and month in the url are optional. But whenever I try to go to the address:
localhost:3000/page/contact_us
I get an error. It's only when I enter both a year and a month that I don't get an error. For example,
localhost:3000/page/contact_us/2014/11
works!
Rails tells me that the error is in the contact_us.html.erb file. The error line is:
<%=contact_us(#month,#year).html_safe%>
The contact_us(month, year) function is defined in a helper file - page_helper.rb
The idea is that 2 arguments are usually passed above (in the url), but sometimes 1 or no arguments may be passed in the url. I get an error when less than 2 arguments are passed.
Please help! I'm using rails 4.1.8 and Rubymine
Your invalid date error is coming from the calendar method that you included a link to you in your comments. You have:
def calendar(month, year)
current_date = Date.new(year, month, 1)
...
The problem is that if month or year is nil, you are basically doing this (in this case, assuming both are nil):
current_date = Date.new(nil, nil, 1)
Just run rails console and try that: you get this error - TypeError: no implicit conversion from nil to integer.
So the problem isn't with your url, its before. You could add some lines like this to fix that error:
def calendar(month, year)
month ||= 1
year ||= 1900
current_date = Date.new(year, month, 1)
...
...but then you'll have to keep track of the fact that those aren't correct dates so that they don't get passed in the url, wherever that happens.
NOTE: This is my first post, so please be kind. I'm a rails newb so it's very plausible that i'm missing something simple.
I'm working with a Ruby on Rails application locally and am getting this ArgumentError when attempting to fill out a test form:
invalid argument to TimeZone[]: nil
Application Trace shows the following:
app/models/venue.rb:117:in `timezone'
app/controllers/events_controller.rb:80:in `block in create'
Lines 116-118 in venue.rb:
def timezone
ActiveSupport::TimeZone.new(timezone_name)
end
Lines 78-87 in events_controller.rb:
["start", "end"].each do |t|
month, day, year = params["event"]["#{ t }_date"].split("/")
#event.send("#{ t }s_at=", #event.venue.timezone.local_to_utc(Time.utc(
("20" + year).to_i,
month.to_i,
day.to_i,
military_hours(t),
params["#{ t }_minute"].to_i,
0
)))
The form works correctly on the live site, so its possible that i setup my dev environment improperly. Can anyone point me in the right direction?
UPDATE
class Venue
key :timezone_name, String
def set_timezone_name
tz = Timezone.contains(self)
self.timezone_name = tz.name if tz
end
It would help if you provided more context for this question. I'll assume that set_timezone_name is called to initialize the timezone_name value when there is none set. It's then likely in this case set_timezone_name fails to set a value. You could try providing a fallback to guarantee a value is set. Example:
def set_timezone_name
tz = Timezone.contains(self)
self.timezone_name = tz.try(:name) || “Eastern Time (US & Canada)”
end
I need to update time info in a DateTime.
I get a string in the format "14" or "14:30" (for example), so I need to give it to Time parser to get the right hour. Then I need to update self.start_at which is a datetime which already has a time, but I need to update it.
self.start_at_hours = Time.parse(self.start_at_hours) # example 14:30:00
# NEED TO UPDATE self.start_at which is a datetime
I was using the change method on self.start_at but it only takes hour and minutes separated and I'm not sure what should I do.
Have you thought about doing somethings like this?
time_to_merge = Time.new
date_to_merge = Date.today
merged_datetime = DateTime.new(date_to_merge.year, date_to_merge.month,
date_to_merge.day, time_to_merge.hour,
time_to_merge.min, time_to_merge.sec)
For replacing time, the .change() method should work, like this:
my_datetime = my_datetime.change(hour: my_time.hour, min: my_time.min, sec: my_time.sec)
For adding time, try converting to seconds and then adding them:
my_datetime += my_time.seconds_since_midnight.seconds