Ruby日期比较不起作用?

时间:2017-02-08 16:01:05

标签: ruby

我试图确定过去一周是否发生了什么事。我使用Time.now.last_week.sunday获取本周的开始时间并尝试与>运算符进行比较。

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

我遇到的问题是第二天发生的事情仍被过滤掉:

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

1 个答案:

答案 0 :(得分:1)

sunday Time类不是Date。尝试将其解析为这样的日期。

sunday = Time.now.last_week.sunday.to_date