Jones
Jones

Reputation: 1214

Ruby date comparison not working?

I'm trying to determine if something happened in the past week. I'm gettign the begining of the week with Time.now.last_week.sunday and trying to compare with the > operator.

sunday = Time.now.last_week.sunday
puts "Last Sunday:  " + sunday.to_s
json = json["objects"]
puts "JSON objects found: " + json.count.to_s
puts "checking date"
json.select! do |statement|
  puts "\tObject date: " + Date.parse(statement["statement_date"]).to_s
  puts "\tSince Sunday? " + (Date.parse(statement["statement_date"]) > sunday).to_s
  (Date.parse(statement["statement_date"]) > sunday)
end

The problem I run into is that things happening the day after, are still filtered out:

Last Sunday:  2017-02-05 23:59:59 -0500
JSON objects found: 10
checking date
    Object date: 2017-02-06
    Since Sunday? false
    Object date: 2017-02-02
    Since Sunday? false
    Object date: 2017-02-06
    Since Sunday? false
    Object date: 2017-02-05
    Since Sunday? false
    Object date: 2017-02-01

Upvotes: 0

Views: 51

Answers (1)

Lukas Baliak
Lukas Baliak

Reputation: 2869

sunday is Time class not Date. Try to parse it into date like this.

sunday = Time.now.last_week.sunday.to_date

Upvotes: 1

Related Questions