Ruby - Date Time Times

Introduction

Ruby provides a class called Time to handle date and time.

Let's look at how to use the Time class:

Demo

puts Time.now

Result

Time.now creates an instance of class Time that's set to the current time.

You can manipulate time objects by adding and subtracting numbers of seconds to them.

For example:

Demo

puts Time.now  
puts Time.now - 10 
puts Time.now + 86400

Result

Here, you print the current time, and then the current time minus 10 seconds, and then the current time with 86,400 seconds (exactly one day) added on.

Related Topics