×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

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:

  • Date
  • DateTime
  • Time

If you are working with dates, you are required to understand some of the terms which are related to date such as:

  1. Calendar date: You must be familiar with the term. It is a specific day of a calendar month of a distinct year.
  2. Ordinal date: When you identify a specific date of a year with the help of an ordinary year. It is an ordinal date.
  3. 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.
  4. 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,

  1. new: new method will create a new object depending upon the parameters passed.
  2. jd: It is provided with the local time and returns the Julian day number.
  3. ordinal: It creates a date object depending upon the parameter passed. The parameter may be negative or positive but can never be zero.
  4. commercial: It creates a date object depending upon the provided week date. The parameter can be negative or positive but can never be zero.
  5. parse: It creates a date object and parses the representation of date and time.
  6. 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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.