liveBook · Manning

Timestamp

As explained in The Well-Grounded Rubyist, Fourth Edition

DateTime is a Ruby class provided by the date library for representing calendar dates together with times and related calendar information.

Ruby’s DateTime class represents calendar dates together with times. Load it with `require 'date'`; create values with constructors such as `DateTime.new`, `DateTime.now`, or `DateTime.parse`. section 8.4 section 8.4.1

DateTime supports common date-and-time operations:

  • Query fields such as year, hour, minute, and second, or test the day of the week. section 8.4.2
  • Convert to Date or Time; when the target type needs information the source lacks, missing fields are set to zero. section 8.4.4
  • Add or subtract days with `+` and `-`, and move by months with `<<` and `>>`. section 8.4.4
>> puts DateTime.new(2009, 1, 2, 3, 4, 5)
2009-01-02T03:04:05+00:00
=> nil
>> puts DateTime.now
2025-10-03T15:45:29-04:00
=> nil
>> puts DateTime.parse("October 23, 1973, 10:34 AM")
1973-10-23T10:34:00+00:00

Compare constructing, getting the current date and time, and parsing a date-time string. section 8.4.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains construction, queries, conversion, and calendar arithmetic without surveying every available method.
Examples
Uses DateTime construction, current-time lookup, string parsing, field queries, and month arithmetic.
What you can do after
Apply DateTime constructors, queries, conversions, and calendar arithmetic in Ruby.
How it connects
Connects DateTime with Date and Time through shared date/time operations and conversion methods.

Also explained in

Compare how 10 books explain timestamp →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.