While programming, we often face a scenario where we need to format the ‘Time’ in a clear way. The strftime function allows you to display dates and times in Ruby effectively.
In this article, we will learn about ‘Time Formatting with strftime Function in Ruby.’ We will discuss ‘what is strftime function,’ its syntax, and its uses. We will also discuss how to implement a program using an example.
So all spotlights’ are on ‘strftime’, which brings you the power of formatting time. Moving forward, let’s discuss Time Formatting with strftime Function in Ruby.
Strftime Function in Ruby
Strftime is a built-in function in Ruby that formats dates and times into strings. It consists of one parameter, i.e., ‘format string’. The format string refers to the collection of special characters and format codes. These special characters and format codes depict how the time object must look after being converted into strings.
Below is the syntax for the strftime Ruby function:
Time.strftime(formatString)
In the above syntax, the ‘Time’ is a built-in class in Ruby. It depicts the date and time.
This class allows us to customize and work with real-time data. The time class consists of the method ‘strftime’ used for converting the time object into a string by accepting format string as its parameter. The function then returns the formatted string.
Example
Suppose you wish to display the date in the format: “YYYY-MM-DD”.To do this, you may pass ‘%Y-%m-%d’ as the parameter in the strftime function, such as: NinjaTime=time.strftime(“%Y-%m-%d”)Here ‘%Y’, ‘%m’, ‘%d’ are the format directives. The directives correspond to the specified values in the ‘time’ object and will give the formatted string in the output.
Let's implement strftime() in Ruby.
Time Formatting with strftime Function in Ruby
To implement time formatting with the strftime function in Ruby, we need to follow the following steps:
Steps
Step1: load the ‘time’ library in your program
Step 2: Declare time using Time.new() and store it in a variable
Step3: Use the strftime function such that variable.strftime(FormatString)
Example1:
Code
# loading library
require 'time'
NinjaTime=Time.new();
puts "NinjaTime without strftime: #{NinjaTime}\n\n"
puts "NinjaTime using strftime: #{NinjaTime.strftime("%d/%m/%Y %I:%M %p")}\n\n"
You can also try this code with Online Ruby Compiler
In the above code, first, we have declared Time using Time.new() and stored it in a variable, NinjaTime.
Next, we have displayed the time both with and without the strftime function. The parameters, "%d/%m/%Y %I:%M %p", in the strftime function mean:
%d→date
%m→month
%Y→year
%I→Hour of the day (01…12)
%M→Minute of the hour
%p→meridian indicator (AM or PM)
Ruby Strftime Function Sheet
Symbol
Meaning
Example
%a
Weekday → Sun, Mon…
Thu
%A
Weekday → Sunday, Monday…
Wednesday
%b
Month → Jan, Feb…
Sep
%B
Month → January, February….
September
%c
Date and time representation
Sunday Aug 08 00:00:00 2023
%C
Century number
20
%d
Day of the month
%D
Date in format → mm-dd-yy
05-09-23
%e
Month Day as a decimal number
1
%F
Date format→yyyy-mm-dd
2023-08-08
%H
Hour of the day →(00…23)
00
%I
Hour of the day → (01…12)
12
%j
Day of the year→ 3 digit integer
005
%m
Month→ 2 digit
09
%M
Minute of the hour→ 2 digit
00
%n
Newline
%p
AM / PM
AM
%r
Time in → AM/PM
12:00:00 AM
%R
Time→24 hour format
12:00:00 AM
%s
Unix timestamp
1577836800
%S
Second→ as 2 digit integer
09
%T
Time → 24-hour format with seconds
00:00:00
%%
literal→’%’ character
%
Uses of Strftime Function in Ruby
Some of the major uses of the Strftime Function in Ruby are as follows:
You can use it to format dates and times according to your need. The function helps inenhancing the user interface as it depicts the date in a clear and readable format.
You can use the function to generate time stamps using Time.now. These represent a particular point in time and are used for recording and identifying the time the particular event took place, as shown in the above example.
The strftime function allowsformatting dates and times based on the specific region. For example, in the united states, the date is represented as ‘%m/%d/%Y’; in France, it's '%d/%m/%Y'. Therefore it is very flexible.
Frequently Asked Questions
What is the Strftime function in Ruby?
Strftime is a built-in function in Ruby that formats dates and times into strings. It consists of one parameter, i.e., ‘format string’. The format string refers to the collection of special characters and format codes.
How to use the Strftime function in Ruby?
To use the Strftime function in ruby, you must load the ‘time’ library in your program and declare time. In the strftime function, pass the desired format string.
What is the use of Strftime function in Ruby?
You can use it to format dates and times according to your need. The function helps in enhancing the user interface as it depicts the date in a clear and readable format. You can also use the function to generate time stamps.
What is the format for strftime 24 hour time?
Strftime and strptime are the two functions in Ruby that help in formatting date strings. They consist of format directives that are used according to the specific string format.
What is Time class in Ruby?
‘Time’ is a built-in class in Ruby that depicts the date and time.This class allows us to customize and work with real-time data. The time class consists of the method ‘strftime’ used for converting the time object into a string by accepting format string as its parameter.
Conclusion
In this article, we have learned about Time Formatting with strftime Function in Ruby with examples and code; we have also discussed its significant uses. To learn further, refer to the following links:
You can read more such descriptive articles on our platform, Coding Ninjas Studio. You will find straightforward explanations of almost every topic on the platform. So take your coding journey to the next level using Coding Ninjas.