Format Ruby time as ISO 8601
How Ruby's Time#iso8601 turns a time into a standard timestamp string, what it keeps and drops, and when a readable local format is the better choice.
Converting a Ruby Time to ISO 8601 produces a fixed-order timestamp such as 2026-10-31T14:05:09Z. Text styling tools swap characters for decorative look-alikes. This conversion is different: it changes how a value is represented, not how it looks. This review covers what the conversion changes, whether it suits your purpose, and how the result behaves when it is copied, shared or read aloud.
Is ISO 8601 suitable for your use?
ISO 8601 is the right choice when a machine stores, sorts, exchanges or parses the time. Typical cases are logs, APIs, JSON payloads, database exports, filenames and configuration. Its main benefit is that it cannot be misread. The year, month and day always appear in the same order, and the UTC offset is written out, so nobody has to guess whether 03/04 means March or April. The decisive tradeoff is what it leaves out. The string records an offset, not a named time zone, and Ruby's default output drops fractional seconds. When a person is the audience, such as in an interface, an email or a printed schedule, a localized form like "Saturday, October 31, 2026, 10:05 AM EDT" reads more naturally. A common pattern is to store and transmit ISO 8601 and convert it to a localized format only for display.
The basic methods
require "time" # needed on older Rubies; harmless on newer ones
t = Time.utc(2026, 10, 31, 14, 5, 9, 123_000)
t.iso8601 # => "2026-10-31T14:05:09Z"
t.iso8601(3) # => "2026-10-31T14:05:09.123Z"
local = Time.new(2026, 10, 31, 10, 5, 9, "-04:00")
local.iso8601 # => "2026-10-31T10:05:09-04:00"
require "date"
Date.new(2026, 10, 31).iso8601 # => "2026-10-31"
Time#iso8601 is an alias of Time#xmlschema. Its optional argument sets how many fractional-second digits to include. A time in UTC ends with Z, and any other time ends with a sign and an hours:minutes offset. In older Ruby versions these methods come from the standard library's time extension, so you need require "time". More recent versions provide xmlschema in core. Check the documentation for your Ruby version; keeping the require line does no harm either way.
t.strftime("%Y-%m-%dT%H:%M:%S%:z") # => "2026-10-31T14:05:09+00:00"
t.strftime("%FT%T.%L%:z") # => "2026-10-31T14:05:09.123+00:00"
strftime gives you full control over the output, but for UTC it writes +00:00 rather than Z. Both forms are valid ISO 8601, yet the two strings are not identical. That matters if you compare timestamps as text or deduplicate them. In Rails, ActiveSupport::TimeWithZone also responds to iso8601. JSON encoding of times uses ISO 8601 with millisecond precision by default, and you can change that with ActiveSupport::JSON::Encoding.time_precision.
What the conversion actually changes
A Time object holds an instant, the offset in effect and possibly sub-second precision. Calling iso8601 turns that object into a String made of ordinary ASCII characters: digits, hyphens, colons, the letter T, and either Z or a signed offset. The conversion involves no Unicode character substitution, no mirrored glyphs and no bidirectional control characters. "Format" here means serialization, not font or visual styling.
- Kept: the instant, to the whole second by default, and the UTC offset in effect.
- Dropped by default: fractional seconds. Pass a digit count, such as iso8601(3) or iso8601(6), to keep them.
- Always dropped: the named zone, such as America/New_York, along with its daylight-saving rules. An offset of -04:00 fits several zones, so store the zone name separately if you need to schedule future or recurring local events.
- Changed: the type. A String cannot do time arithmetic, so parse it back into a Time before calculating with it.
Watch the local clock. Time.now uses the local zone of the Ruby process, which usually comes from the TZ environment variable. The same instant can therefore be formatted with different offsets on a laptop and on a server. Call .utc before formatting when you need consistent strings across machines.
Parsing it back
require "time"
s = Time.now.utc.iso8601(6)
back = Time.iso8601(s)
Time.iso8601 is strict and expects the XML Schema style of ISO 8601. Time.parse is lenient and guesses at the format, so it can misread ambiguous input. Use the strict parser for timestamps your own code produced. Also check precision. If you formatted without fractional digits, the parsed time will not equal the original, and an equality check with == can fail.
Will the result stay readable and usable after copying or sharing?
Copying and pasting: the output is plain ASCII, so the characters themselves survive copying between editors, terminals, chat tools and documents. The usual exception is software that recognizes date-like text and converts it into its own date type. Spreadsheets are the common example: they may change the displayed text or discard the offset. This behavior depends on the application and its settings, and no scoped test of a particular destination was supplied here. Check the specific tool you use, and import the column as text if the exact string matters.
Search and sorting: when strings share one offset and one precision, sorting them alphabetically also sorts them chronologically, and they are easy to search with grep or a database query. That property breaks if you mix Z with +00:00, mix different offsets or mix precisions. Normalize to UTC with a fixed number of fractional digits.
Accessibility: a raw timestamp can be hard to follow for someone using assistive technology. Screen readers may announce the hyphens, the T and the offset in ways that are hard to parse. No documentation or test for a specific screen reader was supplied, so treat this as a risk to check rather than a confirmed behavior. On web pages, the documented HTML pattern is the time element. It puts the machine-readable value in the datetime attribute and readable text inside the element.
<time datetime="2026-10-31T14:05:09Z">Saturday, October 31, 2026, 2:05 PM UTC</time>
Clock notation and localization: ISO 8601 uses a 24-hour clock and puts the year first. Many US readers expect a 12-hour clock and month-first dates. That mismatch does not matter for data, but displays should be localized. Ruby's strftime produces English month and weekday names regardless of the system locale. For translated output, use a localization layer such as the I18n gem's l helper in Rails.
When a plain readable format is the better choice
- Customer-facing screens, emails and printed material, where a localized date and a 12-hour or 24-hour clock matching the reader's convention is clearer.
- Text likely to be read aloud or by a screen reader, unless the machine value sits in markup such as the time element.
- Recurring or future events in a local zone, where you should store the local time together with the zone name instead of a fixed offset.
- Protocols that expect a different format. Email headers use RFC 2822 dates (Time#rfc2822), and HTTP headers use HTTP dates (Time#httpdate). Both methods come from the same time extension.
Evidence gaps
No subject-specific test results were supplied. The examples reflect documented Ruby behavior but were not run against a particular Ruby version for this review, so confirm them in your environment. Claims about how spreadsheets, other destination tools or specific screen readers handle ISO 8601 strings are flagged as risks to verify, not observed results.