Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

Ruby Date and Time

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The class Time and Date in Ruby deals with the dates and times. It helps us to get the current time and date according to our system configurations. It also gives us the components of a date and time and also to format time and date.
The date is the day of a month or year that is represented by a number. Date consists of a month, year, date. The date objects are created with ::new, ::parse, ::today, ::jd, ::strptime, ::ordinal, ::commercial etc. All the date objects are unchangeable.

Getting current date and time.
Example :

time1 =
puts "Current Time : " + time1.inspect


Current Time : 2019-09-05 04:09:21 +0000

In above example inspect function is used to get current date and time.

Getting Components of Date and Time.

The Time object can be used to get various components of Date and Time.
Example :

# Ruby program to getting Date and Time
time =
# Components of a Time
puts "Current Time :"+ time.inspect
puts time.year    # => Year of the date 
puts time.month   # => Month of the date (1 to 12)
puts     # => Day of the date (1 to 31 )
puts time.wday    # => 0: Day of week: 0 is Sunday
puts time.yday    # => 365: Day of year
puts time.hour    # => 23: 24-hour clock
puts time.min     # => 59
puts time.sec     # => 59
puts time.usec    # => 999999: microseconds
puts    # => "UTC": timezone name


Current Time :2019-09-05 04:08:27 +0000

There are following terms used in date:

  1. Calendar date: The calendar date is specific day of a calendar.
  2. Ordinal date: The ordinal date is a specific day of a calendar year.
  3. Week date: The week date is a date which represent week in the calendar.
  4. Julian day number: The Julian day number is in passed day since noon(Greenwich Mean Time) on January 1,4713 BCE(in the Julian calendar ).
  5. Modified Julian day number: The Julian day number is in passed day since midnight(Coordinated universal Time) on November 17,1858 CE(in the Gregorian calendar).

Below is the example to get date.
Example :

# Ruby program to illustrate different methods of date
# require ‘date’ is use to print date on screen
require 'date' 
# print Julian day number           
puts Date.jd(2451377)   
# print commercial date  
puts Date.commercial(2019,5,2)     
puts Date.strptime('07-08-2018', '%d-%m-%Y'
# print ordinal date
puts Date.ordinal(2018,15)    



Time and date Directives:

The directives used in the strftime method are:

  • %a: The abbreviated weekday name (for example: Sun ).
  • %A: The full name of the weekday (for example: Sunday).
  • %b: The abbreviated name of the month(for example: Jan)
  • %B: The full name of the month(for example: January )
  • %c: The selected local date and time representation
  • %d: Day of the month (1 to 31).
  • %H: 24-hour clock
  • %I: 12-hour clock
  • %j: day of the year
  • %m: month of the year
  • %M: minute
  • %p: meridian indicator
  • %S: seconds
  • %%: % literal
  • %z: time zone name
  • %Y: year name with the century
  • %y: year name without the century
  • %X: selected representation of time only
  • %x: selected representation of date only
  • %w: weekday
  • %U: week number of current year, starting with first Sunday
  • %W: week number of current year, starting with first Monday

My Personal Notes arrow_drop_up
Last Updated : 06 Sep, 2019
Like Article
Save Article
Similar Reads