In Ruby, date- and time-specific data are handled by two classes:

  • Date
  • DateTime

In order to use these classes, import the date module:



To create a new Date object, use .new():

# With 1 argument
myDate =
# With 2 arguments
myDate =, month)
# With 3 arguments
myDate =, month, day)
myDate =, 07, 30)
puts myDate # Output: 2021-07-30

To create a new DateTime object, use .new():

myDateTime =, month, day, hour, minute, second, offset)
myDateTime =, 07, 30, 13, 46, 45, "+4")
puts myDateTime # Output: 2021-07-30T13:46:45+04:00

To create a new DateTime object set to this very moment, use .now():

puts # Output: 2021-07-30T13:48:56-04:00


Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Ruby on Codecademy