Dates

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

  • Date
  • DateTime

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

require("date")

Syntax

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

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

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

require("date")
myDateTime = DateTime.new(year, month, day, hour, minute, second, offset)
myDateTime = DateTime.new(2021, 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 DateTime.now # Output: 2021-07-30T13:48:56-04:00
Edit this page on GitHub

Contributors

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

Learn Ruby on Codecademy