Strftime in Ruby

Raquel Randall
pushtostart
Published in
5 min readDec 2, 2018

What is time? According to physicists, time is one of the most difficult properties of the universe to understand. According to programmers, Time is a class. And according to my favorite doctor, “it’s more like a big ball of wibbly-wobbly, timey-wimey…stuff.”

My Doctor

The Time class in Ruby represents dates and times stored as the number of seconds that have elapsed since the Epoch, January 1, 1970 00:00 UTC. If you’re like me and still new to programming and computer knowledge, the Epoch is the date when the time started at ‘0’ for Unix computers. It has been used to grab the current time, date and year since that time and until 2038.

In Ruby, you can get the current day and time using ‘Time.new’ or ‘Time.now’, assign that to a variable and call ‘.inspect’ on it to receive the current time and date. You could also just get the year, month, day, hour, minute, second, etc simply by replacing ‘.inspect’ with one of those words.

t = Time.newt.inspect = current time and datet.year = current yeart.day = current hourt.min = current minutet.sec = current second

There are many more ways to modify and show exactly what you want from the Time class. More here: http://ruby-doc.org/core-2.0.0/Time.html

While learning Rails, I had a few assignments that required showing the time and date. It was then that I learned about a way to convert date and time into a human-readable string using the function strftime, which stands for ‘string-format time’. A few different programming languages use strftime, like C++, Python, PHP, and more.

Here’s how it looks in Rails console:

2.3.4 :001 > Time.now
=> 2018-12-02 15:40:48 -0500
2.3.4 :002 > Time.now.strftime("%A %B %d, %Y")
=> "Sunday December 02, 2018"
2.3.4 :003 > Time.now.strftime("It's %I:%M%p on %A %B %d, %Y")
=> "It's 03:45PM on Sunday December 02, 2018"

As you can see, you can also add other string words to make it a coherent sentence.

Here is a reference for using strftime:

%a — The abbreviated weekday name (“Sun”)%A — The full weekday name (“Sunday”)%b — The abbreviated month name (“Jan”)%B — The full month name (“January”)%c — The preferred local date and time representation%d — Day of the month (01 to 31)%H — Hour of the day, 24-hour clock (00 to 23)%I — Hour of the day, 12-hour clock (01 to 12)%j — Day of the year (001 to 366)%m — Month of the year (01 to 12)%M — Minute of the hour (00 to 59)%p — Meridian indicator (“AM” or “PM”)%S — Second of the minute (00 to 60)%U — Week number of the current year, starting with the first Sunday as the first day of the first week (00 to 53)%W — Week number of the current year, starting with the first Monday as the first day of the first week (00 to 53)%w — Day of the week (Sunday is 0, 0 to 6)%x — Preferred representation for the date alone, no time%X — Preferred representation for the time alone, no date%y — Year without a century (00 to 99)%Y — Year with century%Z — Time zone name%% — Literal “%’’ character

Source: https://www.tutorialspoint.com/ruby/ruby_date_time.htm

Rails provides many other cool and easy to use helper methods for time. For example:

2.3.4 :001 > 1.day.ago
=> Sat, 01 Dec 2018 20:55:33 UTC +00:00
2.3.4 :002 > 2.weeks.ago
=> Sun, 18 Nov 2018 20:55:51 UTC +00:00
2.3.4 :003 > 3.months.ago
=> Sun, 02 Sep 2018 20:55:55 UTC +00:00
2.3.4 :004 > 4.years.ago
=> Tue, 02 Dec 2014 20:56:01 UTC +00:00
2.3.4 :005 > Time.now.beginning_of_day
=> 2018-12-02 00:00:00 -0500
2.3.4 :006 > 1.month.ago.to_date
=> Fri, 02 Nov 2018

Another source: https://api.rubyonrails.org/classes/Time.html

Pretty cool, right?

So cool

When storing date and time in your ActiveRecord database, you can use the column type ‘:datetime’, or ‘:time’ for just the time and ‘:date’ for just the date. For my assignment, I wanted to say that doctors and patients are connected through appointments, so I used ‘:datetime’ to show both the time and the date on my practice website. Here is an example of how my ActiveRecord Migration table for appointments looked:

create_table :appointments do |t|
t.datetime :appointment_datetime
t.belongs_to :patient, index: true, foreign_key: true
t.belongs_to :doctor, index: true, foreign_key: true
t.timestamps null: false
end

One of my data files included creating new appointments and using DateTime.new with set numbers; the patient and the doctor in the example are both variables that were previously set.

#in the db/seeds.rb file
Appointment.create([
{appointment_datetime: DateTime.new(2016, 03, 15, 18, 00, 0), patient: homer, doctor: hawkeye},
#in the views/appointments/show.html.erb file
<h2><%= link_to @appointment.patient.name, @appointment.patient %>'s Appointment with <%= link_to "Dr. #{@appointment.doctor.name}", @appointment.doctor %></h2>
<h2>Date and Time: <%= @appointment.appointment_datetime.strftime("%B %d, %Y at %-I:%M%p") %></h2>

I was then able to show the appointment using strftime: %B for the full month, %d for the day of the month, %Y for the full year, %-I for the 12-h hour (the ‘-’ in front makes it a single digit), %M for the minutes, and %p for the meridian indicator.

There’s a slight difference in DateTime and Time, but they show the same outcome when used with strftime.

2.3.4 :001 > Time.new(2016, 03, 15, 18, 00, 0)
=> 2016-03-15 18:00:00 -0400
2.3.4 :002 > DateTime.new(2016, 03, 15, 18, 00, 0)
=> Tue, 15 Mar 2016 18:00:00 +0000
2.3.4 :003 > Time.new(2016, 03, 15, 18, 00, 0).strftime("%B %d, %Y at %-I:%M%p")
=> "March 15, 2016 at 6:00PM"

There’s so much more information out there on the Time class and Rails helper methods, but I hope this post helped other beginners out there looking for a quick and easy time guide.

Doctor Who out!

--

--