Thoughts about tech, programming, and more.

Date, DateTime, Time, and Timezones in Ruby & Ruby on Rails

Helpful methods and explanations of how timezones behave for Ruby and Ruby on Rails Date & Time methods

To Future Me (and anyone else who finds this useful),
I can't seem to ever remember what methods do what in terms of timezone so I have created this cheatsheet for Ruby and Ruby on Rails Date, DateTime, Time and Timezones. Here you go.


Table of Contents

  1. Methods using Local Timezone
  2. Methods Using UTC Time
  3. Ruby on Rails Timezone Support
  4. Format Date & Time to Strings

1. Methods using Local Timezone

Date

Date methods use the local timezone set by application, and if not set by application, then by the application's server

# Parse a string to return a date object in local timezone
Date.parse("January 1 2025")
=> Wed, 01 Jan 2025
# Date object for today's date in local timezone
Date.today
=> Wed, 01 Jan 2025
# Date object for tomorrow's date in local timezone
Date.tomorrow
=> Thu, 02 Jan 2025
# Date object for yesterday's date in local timezone
Date.yesterday
=> Tue, 31 Dec 2024
# Date object with year, month, and day arguments in local timezone
Date.new(2025,2,3)
=> Mon, 03 Feb 2025

# Omitting day will create date object for first day of month
Date.new(2025,2)
=> Sat, 01 Feb 2025

# Omitting month and day will create date object for first day of year
Date.new(2025)
=> Wed, 01 Jan 2025

DateTime

DateTime methods that use the local timezone use the timezone set by application, and if not set by application, then by the application's server

# Parse a string to return a date object in UTC time
DateTime.parse("January 1 2025 10pm")
=> Wed, 01 Jan 2025 22:00:00 +000
# DateTime object current datetime in local timezone
DateTime.now
=> Sun, 05 Jan 2025 12:31:58 -0600

Additional methods are available that can modify the new DateTime object

# Tomorrow's datetime at same time in local timezone
DateTime.now.tomorrow
=> Mon, 06 Jan 2025 12:38:54 -0600

# Yesterday's datetime at same time in local timezone
DateTime.now.yesterday
=> Saturday, 04 Jan 2025 12:38:54 -0600

Time

Time methods that use the local timezone use the timezone set by application, and if not set by application, then by the application's server

# Time object for current time in local timezone
Time.now
=> 2025-01-05 12:35:00.262441 -0600

2. Methods Using UTC Time

DateTime

# DateTime object current datetime in UTC time
DateTime.current
=> Sun, 05 Jan 2025 18:32:14 +0000
# Parse a string to return DateTime object in UTC time
DateTime.parse("January 1, 2025 10pm")
=> Wed, 01 Jan 2025 22:00:00 +0000
# Tomorrow's datetime at same time in UTC time
DateTime.current.tomorrow
=> Mon, 06 Jan 2025 18:39:37 +0000

# Yesterday's datetime at same time in UTC time
DateTime.current.yesterday
=> Sat, 04 Jan 2025 18:39:37 +0000

Time (Ruby on Rails)

# Time object for current time in UTC time
Time.current
=> 2025-01-05 19:50:39.847203000 UTC +00:00

3. Ruby on Rails Timezone Support

Set Application Timezone

# application.rb

class Application < Rails::Application
  # Set the application timezone
  config.time_zone = 'Eastern Time (US & Canada)'
end

Convert UTC to Local Timezone

# UTC datetime to a local timezone
DateTime.current.in_time_zone("Eastern Time (US & Canada)")
=> 2025-01-05 14:53:34.813177000 EST -05:00

Create Time object with a local timezone

Time.zone = "Hawaii"
Time.zone.now
=> 2025-01-05 10:04:16.641573000 HST -10:00

4. Format Date & Time to Strings

DateTime.now.strftime("%b %e, %Y")
=> "Jan  5, 2025

DateTime.now.strftime("%A, %B %e, %Y at %l:%M%P")
=> "Sunday, January  5, 2025 at  2:16pm"

Weekday

  • Full weekday (Sunday): %A
  • Abbreviated weekday (Sun): %a

Month

  • Full month (January): %B
  • Abbreviated month (Jan): %b

Day

  • Day, no leading zeros (7): %e

Year

  • 4 digit year (2025): %Y
  • 2 digit year (25): %y

Hour

  • 12-hour clock (4): %l

Minutes

  • Minutes (43): %M

Meridian Indicator (for 12 hour clock)

  • Uppercase (PM): %p
  • Lowercase (pm): %P

Subscribe to Daniel Lemky

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe