Hello,
(Time.now + 1.year).to_s(:db) works fine => “2007-08-24 20:56:38”
(Date.today + 1.year).to_s(:db) produce wrong result => “88408-06-02”
I wonder why calculations are not possible directly on the Date object
Thanks
Hello,
(Time.now + 1.year).to_s(:db) works fine => “2007-08-24 20:56:38”
(Date.today + 1.year).to_s(:db) produce wrong result => “88408-06-02”
I wonder why calculations are not possible directly on the Date object
Thanks
nuno wrote:
Hello,
(Time.now + 1.year).to_s(:db) works fine => “2007-08-24 20:56:38”
(Date.today + 1.year).to_s(:db) produce wrong result => “88408-06-02”
In Ruby, Date and Time are different types. The + operator for Time
will add seconds. The + operator for Date adds days.
The 1.year is basically just a multiplier of seconds. So, if for some
reason you want to use Date instead of Time, you should convert it to
Time before adding:
(Date.today.to_time + 1.year).to_s(:db)
=> “2007-08-24 06:00:00”
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs