Difference between Date, Time and DateTime

Date 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.
Eg:
$ require 'date'
$ Date.new(2001,2,3)
  Date: 2001-02-03
$ Date.jd(2451944)
  Date: 2001-02-03 ...
$ Date.ordinal(2001,34)
  Date: 2001-02-03 ...
$ Date.commercial(2001,5,6)
  Date: 2001-02-03 ...
$ Date.parse('2001-02-03')
  Date: 2001-02-03 ...
$ Date.strptime('03-02-2001', '%d-%m-%Y')
  Date: 2001-02-03 ...
$ Time.new(2001,2,3).to_date
  Date: 2001-02-03 ...
$ Date.today
  "Mon, 02 Jan 2017"

Time

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
Eg:
$ require 'time'
$ Time.now
   2015-12-08 10:26:40 -0200
$ time = Time.new
  Components of a Time
$ time.year    # => Year of the date
$ time.month   # => Month of the date (1 to 12)
$ time.day     # => Day of the date (1 to 31 )
$ time.wday    # => 0: Day of week: 0 is Sunday
$ time.yday    # => 365: Day of year
$ time.hour    # => 23: 24-hour clock
$ time.min     # => 59
$ time.sec     # => 59
$ time.usec    # => 999999: microseconds
$ time.zone    # => "UTC": timezone name
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.

DateTime

  • 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.
Eg:
$ require 'date'
$ DateTime.new(2001,2,3,4,5,6)
  DateTime: 2001-02-03T04:05:06+00:00 ...

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!

References

 ]]>