Home »
Ruby Tutorial
Date and Time Classes in Ruby
By IncludeHelp Last updated : December 01, 2024
Ruby Data and Time Classes
In any program written in any language, at any point of time you may need to fetch the date and time of the system at that instant. Ruby facilitates you with three classes related to Date and Time namely:
If you are working with dates, you are required to understand some of the terms which are related to date such as:
- Calendar date: You must be familiar with the term. It is a specific day of a calendar month of a distinct year.
- Ordinal date: When you identify a specific date of a year with the help of an ordinary year. It is an ordinal date.
- Week date: It is the combination of calendar week and number of days. The week that includes the first Thursday of the distinct year is the first calendar week of that year.
- Julian day number: It is a kind of progression of date since noon on January 1, 4713 BCE.
Now, let us talk about all those three classes related to date and Time which we have mentioned above in a detailed manner.
1. Date Class
Date class objects are mutable means that they cannot modify themselves at any point of time during the execution process. You will need to include date class in Ruby code before implementing date objects. You can create a date object with ::new, ::parse, ::today, ::jd, ::strptime.
Example
Now let us see, how date objects are created with the help of an example.
=begin
Ruby program to show the implementation of
date class.
=end
require 'date'
puts Date.new(2020,4,12)
puts Date.jd(2451877)
puts Date.ordinal(2020,3)
puts Date.commercial(2020,12,6)
puts Date.parse('2020-02-03')
puts Date.strptime('03-02-2020', '%m-%d-%Y')
Output
2020-04-12
2000-11-28
2020-01-03
2020-03-21
2020-02-03
2020-03-02
Here,
- new: new method will create a new object depending upon the parameters passed.
- jd: It is provided with the local time and returns the Julian day number.
- ordinal: It creates a date object depending upon the parameter passed. The parameter may be negative or positive but can never be zero.
- commercial: It creates a date object depending upon the provided week date. The parameter can be negative or positive but can never be zero.
- parse: It creates a date object and parses the representation of date and time.
- strptime: It returns a hash of parsed elements after parsing the provided representation of date of time along with the specified template.
2. DateTime Class
This class is a subclass of Date class. You can create an object of DateTime class with the help of DateTime.new, DateTime.ordinal, DateTime.parse, DateTime.jd, DateTime.commercial, DateTime.now, and so on. Let us see its example:
Example
=begin
Ruby program to show the implementation of
DateTime class.
=end
require 'date'
d = DateTime.parse('12th Oct 2020 13:37:05+05:40')
puts d.hour
puts d.min
puts d.sec
puts d.offset
Output
13
37
5
17/72
In the above code, we are storing date in an object after parsing it. Then we are calling its multiple methods like an hour, min, sec and offset.
3. Time Class
Time class is an abstraction of Date and DateTime class. You can create its objects with the help of ::new which will use the current system's time. You can pass year, month, day, hour, min, sec in the parameter list. An example is given below:
Example
=begin
Ruby program to show the implementation of
Time class.
=end
time1 = Time.new
puts "Printing Current Time: "+ time1.inspect
puts time1.year
puts time1.month
puts time1.day
puts time1.hour
puts time1.min
puts time1.sec
puts time1.zone
Output
Printing Current Time: 2019-09-25 06:26:58 +0530
2019
9
25
6
26
58
India Standard Time