Time and date

Time.now

Time dot now captures the current moment with date and clock time together. Print it during logging to see exactly when events happen.

Time objects compare with plain operators, so finding the later of two moments needs no extra library. Dates often arrive as text, so the string skills from Encodings pair well with this lesson.

now = Time.now later = now + 60 puts later > now

ruby now_demo.rb

true

Format with strftime

strftime turns a Time into text using percent codes for year, month, day, hours, and minutes. You arrange the codes with any separators you like.

One format string can serve logs, file names, and screen output without extra helpers.

t = Time.new(2026, 10, 6, 12, 30, 0) puts t.strftime("%Y-%m-%d")

ruby format_demo.rb

2026-10-06

Parse input

The time library adds parsing of common formats. Require it first, then pass the user text to parse.

Invalid text raises an error, so validate around parsing when input comes from strangers.

require "time" t = Time.parse("2026-10-06") puts t.year

ruby parse_demo.rb

2026

Date arithmetic

The date library adds calendar math without clock time. today gives the current date, and plus or minus a number shifts by days.

Subtracting two dates gives the gap between them, which to_i turns into whole days.

require "date" today = Date.today week = today + 7 puts today.class puts (week - today).to_i

ruby date_demo.rb

Date
7

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? now = Time.now later = now + 60 puts later > now

    Answer

    true

    Time objects compare with plain operators, and a minute later is greater.

  2. What does this program print? t = Time.new(2026, 10, 6, 12, 30, 0) puts t.strftime("%Y-%m-%d")

    Answer

    2026-10-06

    strftime fills the percent codes, and puts prints the text without quotes.

  3. What does this program print? require "time" t = Time.parse("2026-10-06") puts t.year

    Answer

    2026

    Time.parse reads the text, and year asks for the year component.

  4. What does this program print? require "date" today = Date.today week = today + 7 puts (week - today).to_i

    Answer

    7

    Subtracting two dates gives the gap in days, and to_i turns it into a whole number.

  5. What does strftime do?

    Answer

    Turns a Time into text using percent codes

    It is the formatting direction; Time.parse is the reading direction.

  6. What happens when Time.parse gets text with no time in it?

    Answer

    It raises an ArgumentError

    Parsing fails loudly, which is why input from strangers is validated around it.

  7. What does this program print? p Time.at(0).utc.year

    Answer

    1970

    Time.at(0) is the Unix epoch, and the year of that moment is 1970.

  8. What does this program print? p Time.new(2026, 1, 1).strftime("%d/%m/%Y")

    Answer

    "01/01/2026"

    %d is the day, %m the month and %Y the four-digit year, and p prints the string with quotes.

  9. What does a Date hold that a Time does not?

    Answer

    Only the calendar day, without clock time

    Date is calendar math; Time carries date and clock time together.

  10. What does this program print? require "date" p (Date.new(2026, 1, 10) - Date.new(2026, 1, 1)).to_i

    Answer

    9

    The gap between 1 January and 10 January is nine days.

Article author: Arthur Isaev