Time and dates
“How many days until the holidays?” “What weekday was I born on?” Ruby knows the calendar. Time.now is this very moment – run the cell twice and watch the seconds move:
now = Time.now
p now.year
p now.month
p now.hour
nowA Time on its own looks rather technical. strftime (“string format time”) turns it into text the way you want it: every % letter is a placeholder – %d the day, %m the month, %Y the year with four digits, %H:%M hours and minutes, %A the weekday and %B the month's name:
now = Time.now
puts now.strftime("%d.%m.%Y %H:%M")
puts now.strftime("%Y-%m-%d")
now.strftime("%A, %B %-d")For days on the calendar – birthdays, holidays, deadlines – there is Date. It comes with Ruby but has to be loaded with require "date" first. A Date knows its weekday, and with + and - you count in days:
require "date"
birthday = Date.new(2026, 7, 14)
p birthday.saturday?
p birthday + 7
birthday.strftime("%A")The difference between two Dates is a number of days. Ruby gives it as a fraction like (60/1) – a Date could in theory be cut into hours – and to_i turns it into a plain whole number:
require "date"
today = Date.today
new_year = Date.new(today.year + 1, 1, 1)
p new_year - today
(new_year - today).to_iWhen it comes to names, Ruby speaks English: %A always gives “Thursday”, in every country. Behind each weekday is a number, wday: 0 is Sunday, 1 Monday, up to 6 for Saturday. With an array of your own you can give the days any names you like – German ones, say:
require "date"
german = %w[Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag] # the days in German
christmas_eve = Date.new(2026, 12, 24)
p christmas_eve.wday
german[christmas_eve.wday]Dates can even make a range, just like numbers: (from..to) contains every day in between. How many Saturdays does December 2026 have?
require "date"
december = Date.new(2026, 12, 1)..Date.new(2026, 12, 31)
december.count { |d| d.saturday? }One last quirk of the calendar: leap years. Every fourth year has a 29 February – except every hundredth, except every four hundredth. Date.leap? knows the rule, and Date.valid_date? checks whether a day exists at all:
require "date"
p Date.leap?(2024)
p Date.leap?(1900)
p Date.leap?(2000)
Date.valid_date?(2026, 2, 29)Time is for moments with a clock (when did the train leave?), Date for days on the calendar (when is the party?). Lesson 19 (formatting text) showed format, whose placeholders work much like those of strftime; the timelog track computes with time in earnest in lesson 109.
Date.new(1995, 12, 21). Put the weekday of that day, as a word, into weekday. Then count in days_until how many whole days lie between 2 November 2026 and 24 December 2026 – with two Date.new.require "date"
release = Date.new(1995, 12, 21) # Ruby's first public release
# weekday = ... # the weekday of release as a word, e.g. "Monday"
# days_until = ... # whole days from 2 November 2026 to 24 December 2026
unlucky the months of 2026 (as numbers, 1 to 12) whose 13th falls on a Friday. select over 1..12 and friday? will get you there.require "date"
# unlucky = ... # the months of 2026 (as numbers) whose 13th is a Friday