[Ruby on Rails (RoR)] Use #before? and #after? to compare Date, DateTime, Time or ActiveSupport::TimeWithZone in Rails 6
#before? and #after?
Date
, DateTime
, Time
and ActiveSupport::TimeWithZone
objects using before? and after? in Rails 6.
before?
and after?
are aliases to < (less than) and > (greater than) methods respectively.
Date
1 | > > Date.new(2019, 3, 31).before?(Date.new(2019, 4, 1)) |
DateTime
1 | > > DateTime.parse('2019-03-31').before?(DateTime.parse('2019-04-01')) |
Time
1 | > > Time.parse('2019-03-31').before?(Time.parse('2019-04-01')) |
ActiveSupport::TimeWithZone
1 | > > ActiveSupport::TimeWithZone.new(Time.utc(2019, 3, 31, 12, 0, 0), ActiveSupport::TimeZone["Eastern Time (US & Canada)"]).before?(ActiveSupport::TimeWithZone.new(Time.utc(2019, 4, 1, 12, 0, 0), ActiveSupport::TimeZone["Eastern Time (US & Canada)"])) |
References
[3] Class: Date (Ruby 3.0.0) - https://ruby-doc.org/stdlib-3.0.0/libdoc/date/rdoc/Date.html
[4] Class: DateTime (Ruby 2.6.1) - https://ruby-doc.org/stdlib-2.6.1/libdoc/date/rdoc/DateTime.html
[5] Class: Time (Ruby 3.0.0) - https://ruby-doc.org/core-3.0.0/Time.html