×

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

Ruby program to get the individual components of the current date-time

Last Updated : December 15, 2025

Problem Solution

In this program, we will get the individual components of current date time using year(), month(), day(), hour(), min(), sec() methods.

Program/Source Code

The source code to get the current date-time using inspect() method is given below. The given program is compiled and executed successfully.

# Ruby program to get the individual components 
# of current date time

date_time = Time.new();

curretDateTime = date_time.inspect();

year  = date_time.year();
month = date_time.month();
day   = date_time.day();

hour   = date_time.hour();
minute = date_time.min();
second = date_time.sec();

printf "DateTime: %02d/%02d/%04d %02d:%02d:%02d ",day, month,year,hour,minute,second;

Output

DateTime: 01/02/2022 07:32:18 

Explanation

In the above program, we created the object of Time class and got the individual components of date and time using year(), month(), day(), hour(), min(), sec() methods. After that, we printed the date and time.

Ruby Date and Time Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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