Ruby Strftime Function: How to Format the Date and Time
The 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:
timex = Time.new
puts "Current Time : " + timex.inspect
timey = Time.now
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
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.
- t.strftime(“%a”) => “Fri” # Gives name of week day in short form of the time
- t.strftime(“%b”) => “Apr” # Gives name of month in short form of the time
- t.strftime(“%c”) => Fri Apr 11 14:17:02 2014 # Gives date and time
- t.strftime(“%d”) => 11 # Gives day of month of the time zero padded(01..31)
- t.strftime(“%e”) => 11 # Gives day of month of the time blank padded(1..31)
- t.strftime(“%f”) => %f # not useful
- t.strftime(“%g”) => 14
- t.strftime(“%h”) => Apr # Equivalent to %b
- t.strftime(“%i”) => %i # not useful
- t.strftime(“%j) => 101 # Gives day of the year
- t.strftime(“%k”) => 14 # Gives hour, 24-hour clock, blank-padded ( 0..23)
- t.strftime(“%l”) => 2 # Gives hour, 12-hour clock, blank-padded ( 0..12)
- t.strftime(“%m”) => 04 # Gives month of the year
- t.strftime(“%n”) => newline character (\n)
- t.strftime(“%o”) => %o
- t.strftime(“%p”) => PM # Meridian Indicator, upper case (‘AM’ or ‘PM’)
- t.strftime(“%q”) => %q
- t.strftime(“%r”) => 02:17:02 PM # Gives time, 12-hour (same as %I:%M:%S %p)
- t.strftime(“%s”) => Gives number of seconds since 1970-01-01 00:00:00 UTC.
- t.strftime(“%t”) => tab character (\t)
- t.strftime(“%u”) = > 5 # Gives Day of the week as a decimal, Monday being 1. (1..7)
- t.strftime(“%v”) => 11-APR-2014 # Gives VMS date (%e-%b-%Y)
- t.strftime(“%w”) => 4 # Gives day of the week (Sunday is 0, 0..6)
- t.strftime(“%x”) => 04/11/14 # Gives representation for the date alone, no time
- t.strftime(“%y”) => 14 # Gives year without century of the time
- t.strftime(“%z”) => +0530
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.
- t.strftime(‘%A’) => Friday # Gives week day in full form of the time
- t.strftime(‘%B’) => April # Gives the full month name
- t.strftime(‘%C’) => 20 # year/100 => 2014/100 => rounded to 20
- t.strftime(‘%D’) => Date(%m/%d/%y)
- t.strftime(‘%E’) =>
- t.strftime(‘%F’) => Equivalent to %Y-%m-%d(the ISO 8601 date format)
- t.strftime(‘%G’) =>
- t.strftime(‘%H’) => 14 Gives hour of the time in 24 hour clock format
- t.strftime(‘%I’) => 2 Gives hour of the time in 12 hour clock format
- t.strftime(‘%J’) => 17 Gives Minutes of the time
- t.strftime(‘%K’) =>
- t.strftime(‘%L’) => Millisecond of the second(000…999)
- t.strftime(‘%M’) => Minute of the hour(00..59)
- t.strftime(‘%N’) =>Fractional seconds digits, default is 9 digits (nanosecond)
- %3N millisecond (3 digits)
- %6N microsecond (6 digits)
- %9N nanosecond (9 digits)
- t.strftime(‘%O’) =>
- t.strftime(‘%P) => pm # Meridian Indicator, lower case (‘am’ or ‘pm’)
- t.strftime(‘%Q’) =>
- t.strftime(‘%R’) =>14:17:02
- t.strftime(‘%S’) => 02 Gives Seconds of the time
- t.strftime(‘%T’) => 14:17:02 #Gives 24 hour time
- t.strftime(‘%U’) => Week number of the present year, beginning with the first Sunday as the first day of the first week(01..53)
- t.strftime(‘%V’) => Week number of year according to ISO 8601(01..53)
- t.strftime(‘%W’) => Week number of the present year, beginning with the first Monday as the first day of the first week(01..53)
- t.strftime(‘%X’) => 14:17:02 # Preferred representation for the time alone with no date
- t.strftime(‘%Y’) => 2014 # Gives Year of the time
- t.strftime(‘%Z’) => IST # Gives Time Zone of the time
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.
timex = Time.new
puts "Current Time : " + time.inspect
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). timex.day 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. timex.zone 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.
timex = Time.new
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.