Q:

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

belongs to collection: Ruby Date and Time Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Date and Time Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to get the current weekday... >>
<< Ruby program to get current date-time using inspec...