Is there an add_days in ruby datetime?

后端 未结 6 1297
生来不讨喜
生来不讨喜 2020-12-25 10:59

In C#, There is a method AddDays([number of days]) in DateTime class.

Is there any kind of method like this in ruby?

6条回答
  •  猫巷女王i
    2020-12-25 11:41

    In Rails there are very useful methods of Fixnum class for this (here n is Fixnum. For example: 1,2,3.... ):

    Date.today + n.seconds # you can use 1.second
    Date.today + n.minutes # you can use 1.minute
    Date.today + n.hours # you can use 1.hour
    Date.today + n.days # you can use 1.day
    Date.today + n.weeks # you can use 1.week
    Date.today + n.months # you can use 1.month
    Date.today + n.years # you can use 1.year
    

    These are convenient for Time class too.

    PS: require Active Support Core Extensions to use these in Ruby

    require 'active_support/core_ext'
    

提交回复
热议问题