ADVERTISEMENT
ADVERTISEMENT

Ruby program to demonstrate the time arithmetic

Ruby Example: Write a program to demonstrate the time arithmetic.
Submitted by Nidhi, on February 08, 2022

Problem Solution:

In this program, we will get the current date-time then we will add, remove seconds from the current time and print the result.

Program/Source Code:

The source code to demonstrate time arithmetic is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate time arithmetic

currentTime = Time.now;
print "Current time    : ",currentTime,"\n";

pastTime = currentTime - 20;
print "Past time       : ",pastTime,"\n";

futureTime = currentTime + 20;
print "Future time     : ",pastTime,"\n";

timeDiff = futureTime - pastTime;
print "Time difference : ",timeDiff, " seconds";

Output:

Current time    : 2022-02-08 08:36:32 +0000
Past time       : 2022-02-08 08:36:12 +0000
Future time     : 2022-02-08 08:36:12 +0000
Time difference : 40.0 seconds

Explanation:

In the above program, we created the object of the Time class. Then we get the current date-time using Time.Now() method. After that, we performed arithmetic operations with the current time and printed the result.

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.