Date and Time in Ruby

· 5 min read · Updated March 31, 2026 · beginner
ruby date time stdlib guides

Ruby gives you everything you need to work with dates and times out of the box. Whether you’re timestamps for logging, building a calendar feature, or handling user-localized times, the standard library has you covered.

This guide walks through the core classes, common operations, and practical patterns you’ll reach for every day.

The Time Class

Time is Ruby’s primary class for working with dates and times. It’s built into the language and ready to use without any require statement.

Getting the Current Time

Time.now  # => 2026-03-31 16:00:00 +0000

Time.now returns a Time object representing the current moment in your system’s local timezone.

Creating a Specific Time

Pass year, month, day, and optionally hour, minute, second to create a specific point in time:

Time.new(2025, 6, 15)           # => 2025-06-15 00:00:00 +0000
Time.new(2025, 6, 15, 10, 30)   # => 2025-06-15 10:30:00 +0000
Time.new(2025, 6, 15, 10, 30, 45) # => 2025-06-15 10:30:45 +0000

You can also use the positional argument form:

Time.utc(2025, 6, 15, 10, 30)  # => 2025-06-15 10:30:00 UTC

The Date Class

For pure date work without a time component, Ruby’s Date class lives in the standard library and needs a require:

require "date"

Date.today  # => #<Date: 2026-03-31>
Date.new(2025, 12, 25)  # => #<Date: 2025-12-25>

Date is useful when you’re tracking calendar days and don’t care about hours or minutes.

The DateTime Class

DateTime extends Date with time-of-day support. Use it when you need the precision of Time but want Date-style parsing and arithmetic:

require "date"

DateTime.now          # => #<DateTime: 2026-03-31T16:00:00+00:00>
DateTime.new(2025, 6, 15, 10, 30, 45)  # => #<DateTime: 2025-06-15T10:30:45+00:00>

In practice, Time handles most use cases. Date and DateTime shine when you need date arithmetic or parsing dates without times.

Parsing Strings into Time Objects

Turning a string into a Time object is a daily task — parsing logs, user input, API responses.

Time.parse

require "time"

Time.parse("2025-06-15")              # => 2025-06-15 00:00:00 +0000
Time.parse("2025-06-15 10:30:45")     # => 2025-06-15 10:30:45 +0000

Time.parse uses the current system timezone unless the string includes timezone info.

Date.parse

require "date"

Date.parse("2025-06-15")   # => #<Date: 2025-12-25>
DateTime.parse("2025-06-15 10:30")  # => #<DateTime: 2025-06-15T10:30:00+00:00>

Parsing Without Raising Errors

If the string format is unpredictable, Time.strptime gives you control:

require "time"

Time.strptime("15/06/2025", "%d/%m/%Y")   # => 2025-06-15 00:00:00 +0000
Time.strptime("10:30 AM", "%I:%M %p")     # => current date with 10:30 AM

strptime parses according to the format string you provide rather than guessing.

Formatting Time with strftime

strftime (string format time) formats a Time object into a human-readable string using format directives.

time = Time.new(2025, 6, 15, 10, 30, 45)

time.strftime("%Y-%m-%d")        # => "2025-06-15"
time.strftime("%H:%M:%S")        # => "10:30:45"
time.strftime("%B %d, %Y")       # => "June 15, 2025"
time.strftime("%d/%m/%Y %H:%M")  # => "15/06/2025 10:30"

Common format directives:

DirectiveMeaningExample
%YFour-digit year2025
%mZero-padded month06
%dZero-padded day15
%HHour (24-hour, 00-23)10
%IHour (12-hour, 01-12)10
%MMinute (00-59)30
%SSecond (00-59)45
%pAM or PMAM
%BFull month nameJune
%bAbbreviated monthJun

Timezone Handling

Working with UTC

Time.now.utc  # => 2026-03-31 16:00:00 UTC

Calling utc returns a new Time object in UTC without modifying the original.

Converting Between Timezones

For timezone-aware applications, Ruby’s TZInfo library handles conversions:

require "tzinfo"

tz = TZInfo::Timezone.get("America/New_York")
time = tz.local_time(2025, 6, 15, 10, 30, 45)
time.strftime("%H:%M %Z")  # => "10:30 EDT"

Rails: in_time_zone

If you’re working in a Rails application, in_time_zone converts a Time to a specific zone:

time = Time.now.in_time_zone("Tokyo")
time.strftime("%H:%M %Z")  # => "01:00 JST"

Time Arithmetic

Adding and subtracting time is straightforward — Ruby handles the math for you.

Adding Time

future = Time.now + 3600          # 3600 seconds = 1 hour from now
Time.new(2025, 6, 15) + 86400     # 86400 seconds = 1 day later

Subtracting Time

past = Time.now - 86400           # 1 day ago
difference = Time.now - past      # => 86400.0 (seconds between them)

Subtracting Two Times Gives a Float

start = Time.now
sleep(0.5)
finish = Time.now
duration = finish - start  # => 0.5 (approximately)

The result is always in seconds as a Float.

Date Arithmetic

require "date"

tomorrow = Date.today + 1    # => one day from now
last_week = Date.today - 7   # => seven days ago
days_between = Date.new(2025, 6, 15) - Date.new(2025, 6, 10)  # => 5

Comparing Times

All time classes support standard comparison operators.

early = Time.new(2025, 1, 1)
late  = Time.new(2025, 12, 31)

early < late   # => true
late > early   # => true
early == early # => true

You can sort an array of times:

times = [Time.new(2025, 6, 15), Time.new(2025, 1, 1), Time.new(2025, 12, 31)]
times.sort  # => [2025-01-01, 2025-06-15, 2025-12-31]

Unix Timestamps

A Unix timestamp is the number of seconds since January 1, 1970 UTC. It’s the lingua franca of time representation across systems.

Getting the Current Timestamp

Time.now.to_i  # => 1743446400 (example)

Creating Time from a Timestamp

Time.at(1743446400)  # => 2026-03-31 16:00:00 +0000

Millisecond Timestamps

Many APIs return milliseconds. Divide by 1000 when converting:

Time.at(1743446400000 / 1000.0)  # => 2026-03-31 16:00:00 +0000

And when you need milliseconds:

Time.now.to_f * 1000  # => 1743446400000.0

Extracting Components

Pull individual parts out of a Time object:

t = Time.new(2025, 6, 15, 10, 30, 45)

t.year    # => 2025
t.month   # => 6
t.day     # => 15
t.hour    # => 10
t.min     # => 30
t.sec     # => 45
t.wday    # => 0 (Sunday, 0-6)
t.yday    # => 166 (day of year)
t_zone    # => "UTC"

See Also