Ruby: Get Current Date and Time – Examples & Explanation

In Ruby, the date library which includes the DateTime, Date and Time classes. These classes are specially created to work with dates and times in Ruby programs. The main objective of this article is to facilitate the learners with the most common use of these DateTime, Date and Time classes of Ruby language.

In this article you will learn:

  • How to access and use the DateTime class in Ruby
  • Use of DateTime in Ruby
  • Getting particular Date, Day, Month and Year in Ruby
  • DateTime and its methods in Ruby
  • Get the difference between two dates and times in Ruby
  • Date and Time mathematics or calculation in Ruby
  • Ruby Date and Time Formatting Directives or Specifiers
In Ruby, to access the DateTime class which is located inside the Ruby library, you must include this library by using require command.
See the example below:

# import date library

# date library includes DateTime class
require 'date'

Get current date and time using DateTime class in Ruby

For a simplified way to access the current date and time.
Please see the example given below:

# import date library
require 'date'

# store current date and time in variable x
x = DateTime.now

# print value of variable x
puts x

Output:

2016-07-02T22:43:26+00:00

Similarly, the use of Time class will also produce the same result as shown in the example below:

t = Time.now

puts t

Output:

2016-07-02 22:41:54 +0000

Create a new Date Object from Day, Month and Year in Ruby

In Ruby, you work with dates using Date class. Every exemplary the Date class includes the year, month and date. You can also get a particular year, month or day. For accessing the particular date object in Ruby see the example below:For accessing the particular the date object in Ruby see the example below:

# import date library
require 'date'

# create a new date object from year, month and day
d = Date.new(2016, 06, 29)

# print the value of date object
puts d

Output:

2016-06-29

Getting particular Year, Month and Day from Date object in Ruby

see the code below example:

# import date library
require 'date'

# create a new date object from year, month and day
d = Date.new(2016, 06, 29)

# print the value of date object
puts d

# for getting particular year
puts d.year

# for getting particular month of that year
puts d.month

# for getting particular day of that month
puts d.day

Output:

2016-06-29
2016
6
29

Examples of DateTime class and its Methods in Ruby

There are many methods in DateTime class. Such as year, month, day, wday (week day), yday (day of year), zone (time zone) etc. They can be used by Time object. To call them in Ruby please see the code below:

time = Time.new
puts "Current Time : " + time.inspect

puts time.year    # => this will show the year of the date

puts time.month   # => will show month of the date

puts time.day     # => will show day of the date

puts time.wday    # => will show the day of week: 0 = Sunday and go on

puts time.yday    # => will show the day of the year (365 days)

puts time.hour    # => will show 24 hours of clock 

puts time.min     # => total min of hour

puts time.sec     # => total sec of hour

puts time.usec    # => this will show the microseconds of that hour

puts time.zone    # => this will show time zone (UTC) (GMT)

Output:

Current Time : 2017-07-02 23:00:48 +0000
2017
7
2
0
183
23
0
48
324832
UTC

Get Difference between two dates in Ruby

In Ruby, we can easily get the difference between two particular dates.

require 'date'

start_date = Date.parse "2013-09-14"

end_date =  Date.parse "2013-06-08"

x = start_date - end_date

puts x

Output:

98/1

Note: The above result shows that there is one calculation and in which 98 days difference.

Get Difference between two times in Ruby/Examples of Time Calculation or Mathematics in Ruby

now = Time.now           # this will show the Current time
puts now

past = now - 10          # this will show less than 10 sec from current time
puts past

future = now + 10        # this will show more than 10 sec from current time
puts future

diff = future - now      # this will show the diff in sec in future and current time
puts diff

Output:

2017-07-02 23:08:52 +0000
2017-07-02 23:08:42 +0000
2017-07-02 23:09:02 +0000
10.0

Ruby Date and Time Format Specifiers/Commands

A list of complete Ruby date and time format command is listed below in the table with the explanation.

Serial No Format Command Explanation
1 %a The abbreviated name (Sun)
2 %A The full weekday name (Sunday)
3 %b The abbreviated month name (Jan).
4 %B The full month name (January).
5 %c The preferred local date and time representation.
6 %d Day of the month (01 to 31).
7 %H Hour of the day, 24-hour clock (00 to 23).
8 %I Hour of the day, 12-hour clock (01 to 12).
9 %j Day of the year (001 to 366).
10 %m Month of the year (01 to 12).
11  %M Minute of the hour (00 to 59).
12  %p Meridian indicator (AM or PM).
13  %S Second of the minute (00 to 60).
14  %U Week number of the current year, starting with the first Sunday as the first day of the first week (00 to 53).
15  %W Week number of the current year, starting with the first Monday as the first day of the first week (00 to 53).
16  %w  Day of the week (Sunday is 0, 0 to 6).
17  %x  Preferred representation for the date alone, no time.
18  %X  Preferred representation for the time alone, no date.
19  %y  Year without a century (00 to 99) – Note that y in lower case
20  %Y  Year with the century (like 2015, 2017). Note that Y is in upper case
21  %Z  Time zone name.
22  %%  Literal % character.