Date and time are one of the most important aspects which every coder has to deal with in Ruby. Well, let’s get to know how we keep it up alive and functional.
There are 3 different classes in Ruby that handles date and time. They are Date, Time and DateTime. Date and DateTime classes are both from date library. And Time class from its own time library.

In this article we’ll see how Date and Time works. Let’s have a look at each one of them.


When you need a string format of year, month and day, you have to go through Date class.

  • Has date attributes only (year, month, day)
  • Based on integer whole-day intervals from an arbitrary “day zero”
  • Can handle date arithmetic in units of whole days
  • Date object is created with ::new, ::jd, ::ordinal, ::commercial, ::parse, ::strptime, ::today, Time#to_date etc.
  • Takes 4 bytes to store.



If you need both date and time values, we can make use of Time class.

  • Has date and time attributes (year, month, day, hour, min, sec, subsec)
  • Can handle negative times before unix time
  • Can handle time arithmetic in units of seconds


Also rails provide a really good time class called ActiveSupport::TimeWithZone. It contains all the features the Time class have, plus many improvements, such as the support for configurable time zones.


  • Has date and time attributes (year, month, day, hour, min, sec)
  • It  is formatted as YYYY-MM-DD HH:MM:SS
  • Based on fractions of whole-day intervals from an arbitrary “day zero” (-4712-01-01)
  • Can handle date arithmetic in units of whole days or fractions
  • Takes 8 bytes to store, and has a precision of .001 seconds.
    • A four-byte integer packed as YYYY×10000 + MM×100 + DD
    • A four-byte integer packed as HH×10000 + MM×100 + SS
  • Valid ranges go from the year 1000 to the year 9999
  • It is created with ::new, ::jd, ::ordinal, ::commercial, ::parse, ::strptime, ::now, Time#to_datetime etc.


Let’s see the Differences among all of them which makes them unique.

  • Date use rational and a “day zero” for storage. But Time doesn’t. So Time is faster.
  • Date field is populated with a literal date and does not concern itself with time zones so this can cause trouble if it is not expressed in the user’s local time. A DateTime can always be converted to a user’s local time if required.
  • Time used to track changes to records and update every time when the record is changed. DateTime used to store a specific and static value which is not affected by any changes in records.
  • Time internally converted current time zone to UTC for storage, and during retrieval converted back to the current time zone. DateTime can not do this.
  • Time affected by different TIME ZONE related setting. Datetime is constant.
  • Ruby’s Time class implements a proleptic Gregorian calendar and has no concept of calendar reform. This problem can be overcome using DateTime class.

Now since we know so many facets of Date, Time and DateTime , we can use them in a much better manner in future at our time of needs.

Happy Coding in Ruby!