Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Time.now

now(in: nil)

Creates a new Time object from the current system time. This is the same as Time.new without arguments.

Example
Time.now               # => 2009-06-24 12:39:54 +0900
Time.now(in: '+04:00') # => 2009-06-24 07:39:54 +0400

Parameter:

  • in: zone: a timezone zone, which may be:

    • A string offset from UTC.

    • A single letter offset from UTC, in the range 'A'..'Z', 'J' (the so-called military timezone) excluded.

    • An integer number of seconds.

    • A timezone object; see Timezone Argument for details.

Ruby version changes

  1. 3.1
    • Time.new now accepts optional in: keyword argument for the timezone, as well as Time.at and Time.now, so that is now you can omit minor arguments to Time.new. [Feature #17485]

      Example
      Time.new(2021, 12, 25, in: "+07:00")
      #=> 2021-12-25 00:00:00 +0700
      

      At the same time, time component strings are converted to integers more strictly now.

      Example
      Time.new(2021, 12, 25, "+07:30")
      #=> invalid value for Integer(): "+07:30" (ArgumentError)
      

      Ruby 3.0 or earlier returned probably unexpected result 2021-12-25 07:00:00, not 2021-12-25 07:30:00 nor 2021-12-25 00:00:00 +07:30.

See also .new .parse .local .rfc2822 also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0