Old broken clocks

image by Heather Zabriskie

Using strftime in a Rails view is probably a mistake

The strftime method appears in many languages, all the way back to C. The syntax of the (mostly impenetrable) formatting arguments haven’t changed that much in years and they’re remarkably consistent across languages.

RailsConf 2024

I'm co-chairing RailsConf 2024 in Detroit May 7–9. Come and join us 

Ruby’s version is comprehensively documented in the Time class.

However when strftime is used in your views, there’s a high chance it will lead to confusion and inconsistency.

Instead of…

…using strftime in your views to format dates and times:

<%= @user.last_signed_in_at.strftime("%m-%e-%y %H:%M") %>

Use…

…the built in Rails time and date formats. Or add to them, like I do here, creating my own :stamp format for Date and Time.

config/initializers/time_formats.rb

Date::DATE_FORMATS[:stamp] = "%Y%m%d" # YYYYMMDD
Time::DATE_FORMATS[:stamp] = "%Y%m%d%H%M%S" # YYYYMMDDHHMMSS

Then in the views.

<%= @user.last_signed_in_at.to_s(:stamp) %>

Why?

By using a method with confusing and complex arguments in your views, you’re setting yourself up for inconsistency.

The main benefit to defining global application date and time formats is for the developers who come after you, including future you. The result is a small number of consistent and pre-determined ways to present time throughout your application, and future developers can be spared building their own formats!

Users benefit from this consistency too. If dates and times are always presented in the same way, it takes less time for them to parse. I.e. Ensuring you always say “Jun 11” rather than “11 Jun”; It’s a small thing, but worth sweating the details.

Here’s a couple of useful sites that can help you get to exactly the date formatting you prefer:

For A Good Strftime contains a visually pleasant reference for strftime formatting strings and lets you build a live date format you can use application-wide.

Strftimer lets you paste in a real string representation of a date or time and returns the strftime date format string that would produce that formatting.

I can never remember the Date and Time formats that are provided by default, plus they were hard to find in the documents, so I built a site that spells out the Rails Date and Time formats.

Why not?

There’s no real performance harm in calling strftime in views. This is about organisation and consistency.

Brighton Ruby 2024

Still running UK’s friendliest, Ruby event on Friday 28th June. Ice cream + Ruby 


Last updated on July 14th, 2019 by @andycroll

An email newsletter, with one Ruby/Rails technique delivered with a ‘why?’ and a ‘how?’ every two weeks. It’s deliberately brief, focussed & opinionated.