return data from array of hash based on date range
That doesn't work because:
- You are using
strings
, notdate
objects - You are strictly comparing the ranges (
==
), you need to use (<=
&&>=
)
So, here is a full example, notice the Date.parse
and <= >=
:
require 'date'
params[:start_date] = '2017-01-01'
params[:end_date] = '2017-06-01'
array.select {|e| Date.parse(e[:start_date]) >= Date.parse(params[:start_date]) && Date.parse(e[:end_date]) <= Date.parse(params[:end_date]) }
https://ruby-doc.org/stdlib-2.4.1/libdoc/date/rdoc/Date.html#method-c-parse
UPDATE As pointed by Cary Swoveland there is no need to do Date.parse
, but I think it's a good way to validate the string, especially if it's coming from params.
Select object based on date range
You can use the following:
@days.each do |day|
exits = Exit.where('? BETWEEN start_date AND end_date', day)
# etc.
end
Iterate every month with date objects
I have added following method to Date class:
class Date
def all_months_until to
from = self
from, to = to, from if from > to
m = Date.new from.year, from.month
result = []
while m <= to
result << m
m >>= 1
end
result
end
end
You use it like:
>> t = Date.today
=> #<Date: 2009-11-12 (4910295/2,0,2299161)>
>> t.all_months_until(t+100)
=> [#<Date: 2009-11-01 (4910273/2,0,2299161)>, #<Date: 2009-12-01 (4910333/2,0,2299161)>, #<Date: 2010-01-01 (4910395/2,0,2299161)>, #<Date: 2010-02-01 (4910457/2,0,2299161)>]
Ok, so, more rubyish approach IMHO would be something along:
class Month<Date
def succ
self >> 1
end
end
and
>> t = Month.today
=> #<Month: 2009-11-13 (4910297/2,0,2299161)>
>> (t..t+100).to_a
=> [#<Month: 2009-11-13 (4910297/2,0,2299161)>, #<Month: 2009-12-13 (4910357/2,0,2299161)>, #<Month: 2010-01-13 (4910419/2,0,2299161)>, #<Month: 2010-02-13 (4910481/2,0,2299161)>]
But you would need to be careful to use first days of month (or implement such logic in Month)...
Check if the value is in range between closest array's elements. (ruby)
%w(2015-11-02 2015-11-09 2015-11-16 2015-11-23).any? do |date|
date.to_date.cweek == Date.today.cweek
end
And here is what this does:
First, you have an array of strings, you use any?
to loop through it and check if any fulfils a requirement, then you cast you date strings into actual dates, and cweek gives you the number of a week in the year. Date.today gives you today's date.
Instead of Date.today.cweek
you can use '2015-11-11'.to_date.cweek
.
The loop above returns boolean; you could also get an array of values that fulfil a condition like this:
new_array = %w(2015-11-02 2015-11-09 2015-11-16 2015-11-23).map do |date|
date.to_date.cweek == '2015-11-11'.to_date.cweek
end.compact
Resources:
- Date class on ruby-doc.org
- Date & Time in Ruby on tutorialspoint.com
UPDATE
If you want to get from the database only records with a date from particular week, this is how you could do it:
my_date = '2015-11-11'.to_date
matching_records = MyResource.where( date: my_date.beginning_of_week..my_date.end_of_week )
The assumptions are that you have a model MyResource
, and that it has a column date
. What this does is returns a relation with all the records that have dates from the same week as my_date
.
Get set of hashes from array of hashes between two date values
def get_data(start_date, end_date, hash)
hash.select{|entry| entry["Date"].to_date.between?(start_date.to_date, end_date.to_date) }
end
How to select array elements in a given range in Ruby?
You can use ranges in the array subscript:
arr[100..200]
Convert date range to array of weeks and months
I can think of as below :
require 'date'
months = (Date.parse("2014-01-30")..Date.parse("2014-03-27")).group_by(&:month).map { |_,v| v.first.end_of_month.to_s }
# => ["2014-01-31", "2014-02-28", "2014-03-31"]
weeks = (Date.parse("2014-01-30")..Date.parse("2014-03-27")).select(&:sunday?).map(&:to_s)
# => ["2014-02-02",
# "2014-02-09",
# "2014-02-16",
# "2014-02-23",
# "2014-03-02",
# "2014-03-09",
# "2014-03-16",
# "2014-03-23"]
Ruby: the closest date to specific date
In order for you to get the files to delete:
def old_files(files, date)
files.sort.filter { |file| Date.parse(file) < date }
end
And then you can use:
files = ["2020-09-14.access","2020-09-13.access","2020-09-10.access","2020-09-09.access","2020-09-08.access","2020-09-07.access","2020-09-05.access","2020-09-04.access","2020-09-02.access","2020-09-01.access","2020-09-14.sale","2020-09-12.sale","2020-09-08.sale","2020-09-07.sale","2020-09-06.sale","2020-09-04.sale",]
today = Date.today
date = today -2
to_del_files = old_files(files, date)
Related Topics
Open the Default Browser in Ruby
Ruby - Lexical Scope VS Inheritance
Ruby on Rails: Can You Put Ruby Code in a Yaml Config File
Rails: Unpermitted Parameter in Rails 5
How to Set Up Simple Http Authentication for a Ruby on Rails App on Heroku
Install Rails 3 on Osx with Rvm
Devise - How to Change Setting So That Email Addresses Don't Need to Be Unique
Yaml How Many Spaces Per Indent
What Does 'If _File_ == $0' Mean in Ruby
Rails 4 Unpermitted Parameters for Array
Ruby Datetime Parsing from 'Mm/Dd/Yyyy' Format
Rvm Is Not a Function, Selecting Rubies with 'Rvm Use ...' Will Not Work
How to Install Ruby 1.9.3 in MAC Os X Mountain Lion Without Xcode
Adding an Action to an Existing Controller (Ruby on Rails)
Fail to Bundle Install Puma 4.3.5 or Gem Puma with Ruby-2.6.6 on MACos-10.15.6