Ruby StrftimeThe Ruby programming language was designed by Yukihiro Matsumoto of Japan in 1993. It is similar to scripting languages such as Smalltalk, Perl and Python. Ruby provides a wide range of powerful built in functions. It is a true object oriented language used to develop both Internet and intranet applications. This language provides easy connectivity to various databases such as Oracle, Sybase, DB2 and MySql. Ruby can be installed in both POSIX and Windows environments. It offers scalability and ease of maintenance. The language incorporates support for several GUI tools such as OpenGL, GTK and Tcl/Tk.

A new programmer can learn this major programming language easily and quickly. Ruby code can be embedded into Hypertext  Markup Language (HTML) and used to write Common Gateway Interface (CGI) scripts. Ruby is a general purpose, interpreted and server side scripting language. It is open source but subject to a license. Today we walk you through time and date format function in Ruby programming. Ruby strftime function is an important function that is used to format date and time in Ruby. We assume that you know the basics of Ruby, else  we recommend you take this beginners course on Ruby before we go further.

Time and Date in Ruby

Date and Time are required in many applications. Developers use Date and Time functions to write useful and powerful code. In Ruby, the time class is used to represent date and time. Note that this class may not be able to display dates before 1970 or later than 2038. However this class is widely used in many programs developed using the Ruby programming language. Here’s an example:

#!/usr/bin/ruby -w
timex =
puts "Current Time : " + timex.inspect
timey =
puts "Current Time : " + timey.inspect

In this program, the first line gives the path of the Ruby executable. The program returns the current system date and time. In Ruby, the time object is used to return various components of date and time. To learn more about writing your own programs from scratch in Ruby, you can take this course.

Ruby Strftime Function

This function is used to format date and time in Ruby

strftime(*args) public

The directive has a percent (%) character, zero or more flags, optional minimum field width, optional modifier and a conversion specifier.

The syntax looks like this:


Time Formatting Directives (a-z)

This is an amazing function, in that it has an entire range of options to give you the maximum flexibilty possible.  See the list of options below, with a brief explanation. These are simple enough for you to experiment with on your own, so we won’t get into detailed examples.

To see how these options can be used in actual programs, check out this practical Ruby training.

Time Formatting Directives (A-Z)

If you thought the list above was all you may ever need, think again. Strftime also has a bunch of uppercase directives. Take a look below.

Example: Program to display the time formats

Now that we’ve gone through the huge list of options, lets see how they can actually be used in a program.

#!/usr/bin/ruby -w
timex =
puts "Current Time : " + time.inspect
puts timex.year
puts timex.month
puts timex.wday
puts timex.yday
puts timex.hour
puts timex.min
puts timex.sec
puts timex.usec

In this Ruby program, we extract the various components of the system date and time.timex.year returns of the current of the date. timex.month returns the current month in the numerical format(1 to 12). returns the numerical day of the date(1 to 31). timex.wday returns day of the week (where  0 is sunday and 6 is saturday.) Timex.yday returns the day of the year( it goes from 1 to 365). timex.hour returns current the hour of the 24 hour clock. timex.min returns the current minute(goes from 0 to 59) timex.sec returns the current second( goes from 0 to 59) timex. usec returns the current second expressed in microseconds. returns the system timezone name. If you’d like some more help on writing your own Ruby programs, now would be a good time to take this course.

Time zones and daylight savings time

The Time object in ruby can also be used to return all the information related to Timezones and daylight savings. Take a look at the program that displays the time zones and daylight savings time.

Example 2

#!/usr/bin/ruby -w

timex =
puts timex.utc_offset
puts timex.isdst
puts timex.utc?
puts timex.localtime
puts timex.gmtime
puts timex.getlocal
puts timex.getutc

In this program, time zones are returned.  timex.idst returns false if UTC does not have DST. Timex.UTC? returns true is time is in UTc time zone. timex.localtime converts the time zone  to local time zone. time.gmtime converts local time zone back to UTC. timex.getlocal returns a new Time object which is in local zone. timex.getutc returns a new time object which is in UTC.

Strftime is an important and versatile date and time formatting function in Ruby. It has several options that give programmers’ all possible flexibility. Go through the examples and experiment with them to master this powerful Ruby programming feature. Hope this tutorial on Ruby strftime function was helpful. At any point of time, if you need a refresher on Ruby, you can come back to this Ruby course. Once you’re ready to move on to the next level, this advanced course will help you master Ruby.

Ruby students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo