I'm new in ruby and date arithmetics seems confusing.
How can I remove n months from a DateTime
without using any other modules?
Since DateTime
is a subclass of Date
, you can use <<
or prev_month
:
require 'date'
d = DateTime.now #=> #<DateTime: 2018-02-20T15:39:44+01:00 ...>
d << 4 #=> #<DateTime: 2017-10-20T15:39:44+01:00 ...>
d.prev_month(4) #=> #<DateTime: 2017-10-20T15:39:44+01:00 ...>
Note that DateTime
doesn't account for daylight savings time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With