ADVERTISEMENT
ADVERTISEMENT

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

Ruby Example: Write a program to get the individual components of the current date-time.
Submitted by Nidhi, on February 01, 2022

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!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.