Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

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:

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:



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


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:



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

see the code below example:



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:


Current Time : 2017-07-02 23:00:48 +0000

Get Difference between two dates in Ruby

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



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


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

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 NoFormat CommandExplanation
1%aThe abbreviated name (Sun)
2%AThe full weekday name (Sunday)
3%bThe abbreviated month name (Jan).
4%BThe full month name (January).
5%cThe preferred local date and time representation.
6%dDay of the month (01 to 31).
7%HHour of the day, 24-hour clock (00 to 23).
8%IHour of the day, 12-hour clock (01 to 12).
9%jDay of the year (001 to 366).
10%mMonth of the year (01 to 12).
11 %MMinute of the hour (00 to 59).
12 %pMeridian indicator (AM or PM).
13 %SSecond of the minute (00 to 60).
14 %UWeek number of the current year, starting with the first Sunday as the first day of the first week (00 to 53).
15 %WWeek 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.


LearnAndLearn.com © 2018 - All rights reserved