How we can set datetime which gives same time but different date on each day.
I want to get all objects which are created after 23:00:00 of each day
I have used string interpolation but can this be done in better way.
2.5.1 :013 > created_after = Date.today.strftime("%F") + " 23:00:00"
=> "2020-02-12 23:00:00"
You can try this -
created_after = DateTime.now.in_time_zone(Time.zone).beginning_of_day - 1.hour
or
created_after = DateTime.current.beginning_of_day - 1.hour
Here's another way of doing it:
created_after = DateTime.current.change({hour: 23, min: 0})
I am trying to create a query to record my submissions for this month in my controller using the following code:
#today = Time.now
#thismonth = #today.month
#nextmonth = #thismonth + 1
#submissions = SubmissionLocation.includes(:submission).where(location_id: current_agent.Company_Business_Location, location_id: 2134).map(&:submission)
#submissions_by_group = #submissions.group_by { |m| m.Max_move_in_date.beginning_of_month}.sort_by{:Max_move_in_date}
#submission_this_month = #submissions_by_group.where(:Max_move_in_date => #thismonth)
It seems to be throwing a nomethod error for the where condition in the #submission_this_month instance variable. I understand that it is most likely because my data is now in a hash format, so in that case, how I would I then query it?
I've got some issue with DateTime in Ruby
I've got line which looks like this (it's in .txt file)
DateTime.new(1979,1,1) DateTime.new(2012,3,29)
And my function to get this looks like this
def split_line
array = line.split(' ')
#date_of_birth = array[0]
#date_of_death = array[1]
end
But #date_of_birth and #date_of_death class are String. How can I get them as DateTime?
Assuming your string is in the correct format, then you're probably looking for:
#date_of_birth = array[0].to_datetime
#date_of_death = array[1].to_datetime
See here for more info:
https://apidock.com/rails/String/to_datetime
This:
DateTime.new(1979,1,1) DateTime.new(2012,3,29)
Is not code. What do you expect that to do?
If you want two DateTimes as a space-separated string, do something like:
"#{DateTime.new(1979,1,1)} #{DateTime.new(2012,3,29)}"
When you have something like #{...} inside a set of double quotation marks (they must be double, not single quotation marks), it's called string interpolation. Learn it. Love it. Live it.
But, for the life of me, I don't know why you wouldn't do:
[DateTime.new(1979,1,1), DateTime.new(2012,3,29)]
Which gives you an array, so no split needed. Just:
def split_line
#date_of_birth = array[0]
#date_of_death = array[1]
end
If you want DateTime values, grab the numbers and create them:
require 'date'
'DateTime.new(1979,1,1) DateTime.new(2012,3,29)'.split.map { |s|
DateTime.new(*s.scan(/\d+/).map(&:to_i) )
}
# => [#<DateTime: 1979-01-01T00:00:00+00:00 ((2443875j,0s,0n),+0s,2299161j)>,
# #<DateTime: 2012-03-29T00:00:00+00:00 ((2456016j,0s,0n),+0s,2299161j)>]
The values aren't DateTime though, they're Dates:
'DateTime.new(1979,1,1) DateTime.new(2012,3,29)'.split.map { |s|
Date.new(*s.scan(/\d+/).map(&:to_i) )
}
# => [#<Date: 1979-01-01 ((2443875j,0s,0n),+0s,2299161j)>,
# #<Date: 2012-03-29 ((2456016j,0s,0n),+0s,2299161j)>]
Breaking it down:
'DateTime.new(1979,1,1) DateTime.new(2012,3,29)'.split # => ["DateTime.new(1979,1,1)", "DateTime.new(2012,3,29)"]
.map { |s|
Date.new(
*s.scan(/\d+/) # => ["1979", "1", "1"], ["2012", "3", "29"]
.map(&:to_i) # => [1979, 1, 1], [2012, 3, 29]
)
}
# => [#<Date: 1979-01-01 ((2443875j,0s,0n),+0s,2299161j)>,
# #<Date: 2012-03-29 ((2456016j,0s,0n),+0s,2299161j)>]
* (AKA "splat"), used like this, explodes an array into its elements, which is useful when you have an array but the method only takes separate parameters.
The bigger question is why you're getting values like that in a text file.
What this error could be about? I'm defining an age at my account model:
def age
birthday = DateTime.new(year_Of_Birth.to_i, month_Of_Birth.to_i, day_Of_Birth.to_i)
self.age = ((DateTime.now - birthday) / 365.25).to_i
end
and in my view i get the following error:
ArgumentError - invalid date in the following line:
<dd><%= #account.age %></dd>
Thanks in advance.
Try to test in your Rails app console:
birthday = DateTime.new(1991,4,2) => Tue, 02 Apr 1991 00:00:00 +0000
DateTime.now - birthday => (149073456141953/17280000000)
(DateTime.now - birthday)/365.25 => 23.61926437561592
((DateTime.now - birtday)/365.25).to_i => 23
This works for me. Check the params that you're setting.
I'm almost sure the arguments that you're passing (year_Of_Birth.to_i, month_Of_Birth.to_i, day_Of_Birth.to_i) have something wrong, debug them and show us the values, or if you want you can try this to catch some specific error:
begin
birthday = DateTime.new(year_Of_Birth.to_i, month_Of_Birth.to_i, day_Of_Birth.to_i)
self.age = ((DateTime.now - birthday) / 365.25).to_i
rescue ArgumentError
#your logic
end
Given A Day i-e Monday and a Date e.g: Sat, 09 Aug 2014
how can i get the date of Monday coming immediately after given Date
Update:
def get_date_after(date, day)
return date if date.wday == day.to_date.wday
days_difference = (date - day.to_date).to_i
result = day.to_date + days_difference + (day.to_date.wday - date.wday)
result = result + 1.week if result.to_date < date
end
I was looking for something like above method, calling it would return the date of the day passed coming immediately after the date passed
get_date_after(DateTime.parse("15/09/2014").to_date, "Wednesday") #=> 17/09/2014
The chronic gem allows you to parse date expressions, e.g. Chronic.parse("next Monday"). You can add a reference date with the now option. Here's how it could be used for your method:
def get_date_after(date, day)
Chronic.parse("next #{day}", now: date)
end
In rails you can do it this way
Date.today.next_week(day = :monday)