date - Compare Time.now in Rails but ignore year? -
i have following code in rails 3 application:
scope :active, lambda { where("starts_at <= ? , ends_at >= ?", time.now.utc, time.now.utc) } scope :since, lambda { |hide_time| where("updated_at > ? or starts_at > ?", hide_time.utc, hide_time.utc) if hide_time } def self.display(hide_time) active.since(hide_time) end however, want return results regardless of whether year matches current year or not. long day , month match it's fine. possible?
the starts_at , ends_at columns datetime formatted. automatically include year if dont set 1 in form:
<%= f.datetime_select :starts_at, :order => [:day, :month], :discard_year => true %> any pointers appreciated.
as old pro points out in comments pose problem leap years. want split each dayofyear month(x) , dayofmonth(x) instead.
you can use dayofyear function https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_dayofyear
scope :active, lambda { where("dayofyear(starts_at) <= dayofyear(?) , dayofyear(ends_at) >= dayofyear(?)", time.now.utc, time.now.utc) } scope :since, lambda { |hide_time| where("dayofyear(updated_at) > dayofyear(?) or dayofyear(starts_at) > dayofyear(?)", hide_time.utc, hide_time.utc) if hide_time } in mssql it's
datepart(dayofyear,date)
in postgres it's
extract(doy date)
in sqlite3 it's
strftime("%j",date)
Comments
Post a Comment