Ruby provides built-in classes like Time, Date, and DateTime to work with dates and times.
These classes allow you to create, manipulate, format, and parse date and time values easily.
Note: The Time class is part of Ruby's core library and is available without requiring additional files. However, Date and DateTime are part of the standard library and require 'date' to be used.
Example 1: Create Current Date and Time
You can use the Time class to get the current system time. For example,
current_time = Time.now
puts current_time
Output
2025-05-30 05:42:32 +0000
Here, the Time.now method returns an object representing the current date and time according to the system clock in your system's local timezone.
You then print this object to display the current local date and time.
Note: The timezone shown in outputs may differ depending on your system's local timezone settings. If your system timezone is UTC, you'll see +0000; otherwise, it will show your local timezone offset.
Example 2: Get Current Date
Ruby has a Date class for handling dates (year, month, day).
We require the standard library 'date' to use it. For example,
require 'date'
current_date = Date.today
puts current_date
Output
2025-05-30
In this example, we first included Ruby's date library to access the Date class. Then, we called Date.today to get the current date and print it.
Note: Date.today returns the date according to your system's local time.
Attributes and Methods of Ruby Date & Time Classes
You can retrieve year, month, day, hour, minute, and second components from Date, Time, and DateTime classes.
The most commonly used classes are:
| Class | Description |
|---|---|
Date |
Represents only date (year, month, day). |
Time |
Represents date and time to the second/millisecond. |
DateTime |
Represents date and time with more features, including fractional seconds and timezone. |
Example 3: Extract Date Components
require 'date'
current_date = Date.today
puts "Year: #{current_date.year}"
puts "Month: #{current_date.month}"
puts "Day: #{current_date.day}"
Output
Year: 2025 Month: 5 Day: 30
In this example, we created an object of the Date class named current_date.
Then, we called the year, month, and day methods of the Date class to extract the individual date components.
Example 4: Create a Specific Date Object
You can specify a particular date using the constructor provided by the Date class. For example,
require 'date'
specific_date = Date.new(2022, 12, 25)
puts specific_date
Output
2022-12-25
The Date.new method takes year, month, and day as arguments and returns a Date object for that date.
Example 5: Create a Time Object
You can create an instance of Time that represents a specific date and time. For example,
time1 = Time.new(2022, 12, 25, 10, 30, 45)
puts time1
Output
2022-12-25 10:30:45 +0000
Here, Time.new creates a Time object with the specified year, month, day, hour, minute, and seconds.
Note: If you don't specify a timezone, Time.new uses your system's local timezone.
If some arguments are omitted, Ruby sets the missing parts to default values: hours, minutes, and seconds default to 0 (midnight), and other components default accordingly. For example,
time2 = Time.new(2022, 12, 25)
puts time2
Output
2022-12-25 00:00:00 +0000
Example 6: Get Current Time Components
You can retrieve the hour, minute, and second components from a Time object. For example,
current_time = Time.now
puts "Hour: #{current_time.hour}"
puts "Minute: #{current_time.min}"
puts "Second: #{current_time.sec}"
Output
Hour: 6 Minute: 29 Second: 38
Here, we call the hour, min, and sec methods on the current_time object to extract individual time components.
Ruby DateTime Class
The DateTime class combines date and time functionality, supports fractional seconds, and has enhanced timezone support compared to Time. For example,
require 'date'
date_time = DateTime.new(2022, 12, 25, 10, 30, 45)
puts date_time
# You can also specify a timezone offset as the 7th argument
date_time_offset = DateTime.new(2022, 12, 25, 10, 30, 45, '+05:45')
puts date_time_offset
date_time_now = DateTime.now
puts date_time_now
Output
2022-12-25T10:30:45+00:00 2022-12-25T10:30:45+05:45 2025-05-30T05:42:32+00:00
DateTime.new constructs a DateTime object from the specified date and time components.
By default, the timezone is UTC unless specified explicitly using an offset string (like '+05:45').
Also, DateTime.now returns the current date and time as a DateTime object.
Note: The Time class is usually preferred for most date and time operations as it is more efficient and supports system timezones and daylight saving well.
The DateTime class is useful for historical dates or when manually handling offsets.
Example 7: Date Arithmetic
You can perform various arithmetic operations with Date objects: add or subtract days, find the difference between dates, and multiply or divide durations.
Let's see an example,
require 'date'
date1 = Date.new(2025, 5, 30)
date2 = Date.new(2025, 5, 20)
# Calculate difference in days (as an integer)
date_difference = (date1 - date2).to_i
puts "Difference in days: #{date_difference}"
Output
Difference in days: 10
Example 8: Adding Days to a Date
You can also add days to a Date object using the + operator. For example,
require 'date'
# Adding days to a date
current_date = Date.today
puts "Today: #{current_date}"
puts "In 7 days: #{current_date + 7}"
Output
Today: 2025-05-30 In 7 days: 2025-06-06
More on Ruby Date & Time
You can format date and time objects into strings using strftime. For example,
current_time = Time.now
puts current_time.strftime("%Y-%m-%d %H:%M:%S")
Output
2025-05-30 06:29:38
Here, the code gets the current time and formats it as a string in the YYYY-MM-DD HH:MM:SS format using strftime.
You can parse a string into a DateTime object using strptime. For example,
require 'date'
date_time = DateTime.strptime('25-12-2022 10:30:45', '%d-%m-%Y %H:%M:%S')
puts date_time
Output
2022-12-25T10:30:45+00:00
Here, strptime parses a string according to the given format directives and returns a DateTime object.
You can easily convert standard date and time strings into Ruby objects using Date.parse and Time.parse. For example,
# Parsing with Date.parse and Time.parse
require 'date'
require 'time'
parsed_date = Date.parse("2025-05-30")
puts parsed_date # Output: 2025-05-30
parsed_time = Time.parse("2025-05-30 10:45:00")
puts parsed_time # Output: 2025-05-30 10:45:00 +0000
Here, Date.parse and Time.parse convert string input into date/time objects.