Q:

Ruby program to demonstrate the time arithmetic

belongs to collection: Ruby Date and Time Programs

0

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

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 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.

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 year in four digit...