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.
Program/Source Code:
The source code to demonstrate time arithmetic is given below. The given program is compiled and executed successfully.
Output:
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