Ruby Date & Time

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,

now = Time.now
puts now

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 object for handling dates (year, month, day).

You need to require the standard library 'date' to use it. For example,

require 'date'

today = Date.today
puts today

Output

2025-05-30

In this example, we first include Ruby's date library to access the Date object. Then, we call 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 objects.

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'

today = Date.today
puts "Year: #{today.year}"
puts "Month: #{today.month}"
puts "Day: #{today.day}"

Output

Year: 2025
Month: 5
Day: 30

In this example, we call the year, month, and day methods on a Date object to extract individual date components.


Example 4: Create a Specific Date Object

You can create a Date object representing any specific date using the constructor. For example,

require 'date'

d = Date.new(2022, 12, 25)
puts d

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 a Time object representing a specific date and time. For example,

t1 = Time.new(2022, 12, 25, 10, 30, 45)
puts t1

Output

2022-12-25 10:30:45 +0000

Here, Time.new creates a Time object with the specified year, month, day, hour, minute, and second.

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,

t2 = Time.new(2022, 12, 25)
puts t2

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,

now = Time.now
puts "Hour: #{now.hour}"
puts "Minute: #{now.min}"
puts "Second: #{now.sec}"

Output

Hour: 6
Minute: 29
Second: 38

Here, we call 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.

require 'date'

dt = DateTime.new(2022, 12, 25, 10, 30, 45)
puts dt

# You can also specify a timezone offset as the 7th argument
dt_offset = DateTime.new(2022, 12, 25, 10, 30, 45, '+05:45')
puts dt_offset

dt_now = DateTime.now
puts dt_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 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'

d1 = Date.new(2025, 5, 30)
d2 = Date.new(2025, 5, 20)

# Calculate difference in days (as an integer)
diff = (d1 - d2).to_i

puts "Difference in days: #{diff}"

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
d = Date.today
puts "Today: #{d}"
puts "In 7 days: #{d + 7}"

Output

Today: 2025-05-30
In 7 days: 2025-06-06

More on Ruby Date & Time

Formatting Dates and Times with strftime

You can format date and time objects into strings using strftime. For example,

now = Time.now
puts now.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.

Parsing Date Strings with strptime

You can parse a string into a DateTime object using strptime. For example,

require 'date'

dt = DateTime.strptime('25-12-2022 10:30:45', '%d-%m-%Y %H:%M:%S')
puts dt

Output

2022-12-25T10:30:45+00:00

Here, strptime parses a string according to the given format directives and returns a DateTime object.

Time.parse and Date.parse

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'

d = Date.parse("2025-05-30")
puts d   # Output: 2025-05-30

t = Time.parse("2025-05-30 10:45:00")
puts t   # Output: 2025-05-30 10:45:00 +0000

Here, Date.parse and Time.parse convert string input into date/time objects.

Did you find this article helpful?

Your path to become a builder.

Builders don’t just know how to code, they create solutions that matter. Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community