Dates and Times

Ruby has three separate classes to represent date and time data: Time, Date, and DateTime.

  • Time represents a specific moment in time, and you can retrieve both date and time information based on that. The Time class is based on a library that is common to Unix systems and is used by many programming languages.

  • Date represents a date only with no time information attached. It’s useful for calendar arithmetic that doesn’t depend on the time of day. You need require "date" to use the Date class.

  • DateTime also represents a specific moment in time but uses a different internal representation than Time. DateTime is now considered deprecated—at one point it had a more complete API than Time, but that is no longer true. Currently, ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.