Home » Ruby Date and Time

Ruby Date and Time

by Online Tutorials Library

Ruby Date & Time

Ruby has Mainly three classes related to date and time in its documentation.

  • Date
  • DateTime
  • Time

Date

Ruby date provides two classes, Date and DateTime.

To understand the concept of date, first we need to understand some terms.

  • Calendar date: The calendar date is a particular day within a calendar month within a year.
  • Ordinal date: The ordinal date is a particular day of a calendar year identified by its ordinal number.
  • Week date: The week date is a day identified by calendar week and day numbers. The first calendar week of the year is the one which includes first Thursday of that year.
  • Julian day number: The julian day number is in elapsed day since noon on January 1, 4713 BCE.
  • Modified julian day number: The modified julian day number is in elapsed day since midnight on November 17, 1858 CE.

The Date object is created with ::new, ::parse, ::today, ::jd, ::strptime, etc. All date objects are immutable, hence they can’t modify themselves.

Example:

Output:

Ruby date time 1

The Date object has various methods as shown in the below example.

Example:

Output:

Ruby date time 2


DateTime

Ruby DateTime is a subclass of Date. It easily handles date, hour, minute, second and offset.

The DateTime object created with DateTime.new, DateTime.ordinal, DateTime.parse, DateTime.jd, DateTime.commercial, DateTime.now, etc.

Example:

Output:

Ruby date time 3

The last element of day, minute, second or hour can be fractional.

The DateTime object has various methods as shown in the below example.

Example:

Output:

Ruby date time 4


Time

Time class is an abstraction of dates and times. It is stored internally as the number of seconds since Epoch time. The Time class treats GMT (Grenwich Mean Time) and UTC (Coordinated Universal Time) equivalent.

Times may appear equal but on comparison they may be different as all times may have fraction.

Time implementation uses a signed 63 bit integer, Bignum or Rational. Time works slower when integer is used.


Creating a new Time Instance

A new Time instance can be created with ::new. This will use your current system’s time. Parts of time like year, month, day, hour, minute, etc can also be passed.

While creating a new time instance, you need to pass at least a year. If only year is passed, then time will default to January 1 of that year at 00:00:00 with current system time zone.

Example:

Output:

Ruby date time 5


Time with gm, utc and local functions

Instead of using current system setting, you can also use GMT, local and UTC timezones.

Example:

Output:

Ruby date time 6


Working with an instance of time

After creating an instance of time, we can work on that time in following ways.

Example:

Output:

Ruby date time 7


Timezones and daylight savings time

A Time object can be used to get all the information related to timezones. All the information will be displayed respective to current time of our system.

Example:

Output:

Ruby date time 8


Next TopicRuby ranges

You may also like